首先,我已经在各地进行了研究,以便对此查询进行排序,但我找不到解决问题的答案,所以在这里。
我有这个问题:
SELECT
[Report Date],
COUNT(CASE WHEN [total_ahr_cap] = '0' OR [standing_load] = '0'THEN NULL ELSE 1 END) AS [Zero Values],
COUNT(CASE WHEN [total_ahr_cap] / [standing_load] > '12' THEN 1 ELSE NULL END) AS [Green Zone],
COUNT(CASE WHEN [total_ahr_cap] / [standing_load] < '12' AND [total_ahr_cap] / [standing_load] >= '10'
THEN 1 ELSE NULL END) AS [Yellow Zone],
COUNT(CASE WHEN [total_ahr_cap] / [standing_load] < '10' THEN 1 ELSE NULL END) AS [Red Zone],
COUNT(CASE WHEN [total_ahr_cap] IS NULL OR [standing_load] IS NULL THEN 1 END)AS [Null Values],
COUNT(CASE WHEN [total_ahr_cap] / [standing_load] > '0' THEN 1 ELSE NULL END) AS [Total]
FROM [dbo].[dc_chargers$]
WHERE [Report Date] = 'March 2016' AND sla_no NOT LIKE ('%South%')
GROUP BY [Report Date]
请记住,我复制并粘贴此查询以获取一年中几个月的结果。我期待的结果是计算了多少字段属于&#39; green&#39;黄色&#39;&#39; red&#39;和&#39; null&#39;作为我的老板的值字段每个月都需要此信息,但只要[standing_load]值或[total_ahr_cap]值为0 - 我就会得到除以零的错误。
我需要计算重新除以0的数字,因为我无法将它们更改为NULL
值,所以我怎样才能解决这个问题,因为每次我得到除以零的错误,查询都不会计算具有0 in的数字的月份,我必须将它们更改为NULL
值,但这会给我不准确的数据。
请参阅下面的查询内容:
Report Date Green Zone Yellow Zone Red Zone Null Values Total
2015-12-01 276 5 5 16 286
2016-01-01 365 5 5 24 375
2016-02-01 376 4 5 25 384
正如您所看到的,它会计算所有其他月份,但不计算具有0值的月份。
请帮忙!答案 0 :(得分:2)
To avoid Divide by 0
exception in you query, in each case where you do divide operation first check if divider is greater than 0. Also why comparing numerical values to string?
Also your 5th case
is missing else
part.
See code below:
SELECT
[Report Date],
COUNT(CASE WHEN [total_ahr_cap] = 0 OR [standing_load] = 0 THEN NULL ELSE 1 END) AS [Zero Values],
COUNT(CASE WHEN [standing_load] > 0 and [total_ahr_cap] / [standing_load] > 12 THEN 1 ELSE NULL END) AS [Green Zone],
COUNT(CASE WHEN [standing_load] > 0 and [total_ahr_cap] / [standing_load] < 12 AND [total_ahr_cap] / [standing_load] >= 10
THEN 1 ELSE NULL END) AS [Yellow Zone],
COUNT(CASE WHEN [standing_load] > 0 and [total_ahr_cap] / [standing_load] < 10 THEN 1 ELSE NULL END) AS [Red Zone],
COUNT(CASE WHEN [standing_load] > 0 and [total_ahr_cap] IS NULL OR [standing_load] IS NULL THEN 1 ELSE NULL END)AS [Null Values],
COUNT(CASE WHEN [standing_load] > 0 and [total_ahr_cap] / [standing_load] > 0 THEN 1 ELSE NULL END) AS [Total]
FROM [dbo].[dc_chargers$]
WHERE [Report Date] = 'March 2016' AND sla_no NOT LIKE ('%South%')
GROUP BY [Report Date]
Also a small advice because I'm not sure if it is acceptable in your solution but I'd change count to sum
and nulls to 0.
答案 1 :(得分:0)
try this
SELECT
[Report Date],
sum(CASE WHEN [total_ahr_cap] = 0 OR [standing_load] = 0 THEN 1 ELSE 0 END) AS [Zero Values],
sum(CASE WHEN [total_ahr_cap] / [standing_load] > 12 and [standing_load] is not null THEN 1 ELSE 0 END) AS [Green Zone],
sum(CASE WHEN [total_ahr_cap] / [standing_load] < 12 AND [total_ahr_cap] / [standing_load] >= 10 and [standing_load] is not null
THEN 1 ELSE 0 END) AS [Yellow Zone],
sum(CASE WHEN [total_ahr_cap] / [standing_load] < 10 and [standing_load] is not null THEN 1 ELSE 0 END) AS [Red Zone],
sum(CASE WHEN [total_ahr_cap] IS NULL OR [standing_load] IS NULL THEN 1 ELSE 0 END) AS [Null Values],
sum(CASE WHEN [total_ahr_cap] / [standing_load] > 0 and [standing_load] is not null THEN 1 ELSE 0 END) AS [Total]
FROM [dbo].[dc_chargers$]
WHERE [Report Date] = 'March 2016' AND sla_no NOT LIKE ('%South%')
GROUP BY [Report Date]