I am trying to calculate the percent of total using partition over, and need the result to calculate beyond 6 decimal places. I have millions of records, so I need the calculation to extend beyond 6 decimal places to sum to 100% of total.
I have tried to cast the result as decimal with 18 places, but I am still getting all zeros beyond the 6th decimal place.
Any help would be appreciated!
SELECT
ISNULL(SUM(c.PaidOriginal),0) / SUM(NULLIF(SUM(c.PaidOriginal),0) ) OVER (PARTITION BY c.calyr) AS percentoftotal
,CAST(ISNULL(SUM(c.PaidOriginal),0) / SUM(NULLIF(SUM(c.PaidOriginal),0) ) OVER (PARTITION BY c.calyr) AS DECIMAL(38,18)) AS percentoftotal
FROM c
[screenshot of result]
Update: Thank you for the responses! I have tried casting everything as decimal (the numerator, denominator, and result) and am still getting all zeros past the 6th decimal place. Perhaps the issue is with the Partition by?
CAST(CAST(ISNULL(SUM(c.PaidOriginal),0) AS DECIMAL(38,18)) / CAST(SUM(NULLIF(SUM(c.PaidOriginal),0) ) OVER (PARTITION BY c.calyr)as decimal(38,18)) as decimal(38,18)) AS percentoftotal