我正在使用 SQL Management Studio 2012。我收到“除以零错误”错误。我知道这需要使用 NULLIF 但我不确定如何在我的查询中使用它。
这是我的查询
select
Ward,
LocalAuthority,
Fiscal,
DR,
cast(DR*100.0 /SUM(DR) OVER (
partition by localauthority, fiscal
order by fiscal asc) as Decimal (10,2)) as [DR%]
from (
select
Fiscal,
LocalAuthority,
Ward,
sum(case when code = 'DR' then 1 else 0 end) as DR
from [dbo].[Table]
where datetimeofcall >= '2014-04-01'
group by
ward,
localauthority,f
fiscal
) as A
我实际上已经问过这个问题,之前有人回答得很好,但是没有 OVER() 和 SUM 聚合函数,我对语法感到困惑。我一直在尝试以下的各种含义,但没有成功。
cast(DR*100.0 /NULLIF(SUM(DR,0),1) OVER(partition by localauthority,fiscal order by fiscal asc) as Decimal (10,2)) as [DR%]