When running the below query in SQL Server am getting an error:
Divide by zero error encountered.
select ID, CID, PTYPE, GW,PPE
, payPercentage = SUM(GW) * 100.0 / SUM(SUM(GW)) OVER (partition by ID, PPE)
from pw_part
where ID ='001014055'
group by ID,CID,PPE,PTYPE,GW
payPercentage=SUM(GW) * 100.0 / SUM(SUM(GW)) OVER (partition by ID, PPE) is causing the error
If I change SUM to
ISNULL (SUM(gross_wages) * 100.0 / NULLIF(SUM(SUM(gross_wages)),0),0) OVER (partition by ssn, pay_period_ending)) as pctg
I am facing the following error
The function 'ISNULL' is not a valid windowing function, and cannot be used with the OVER clause.
Thanks in advance
over( partition by ... order by ... rows between ...)and ends just after this last bracket. Place all this text in the first argument ofnullif:nullif(sum(..) over(...), 0)nullifdenominator, soifnull(sum(gw) / /*Should be NULLed for zero*/nullif(sum(sum(gw)) over(), 0), 0)