2016-04-25 92 views
2

首先,我已經研究過無處不在,以便將此查詢排序並且無法找到解決我的問題的答案。除以0的計數

我有這個疑問:

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] 

記住,我複製並粘貼此查詢以獲得數個月的一年的結果。我期待的結果是每個月我的老闆需要這些信息的時候有多少字段屬於「綠色」,「黃色」,「紅色」和「空白」值字段的計數,但每當[standing_load]值或[total_ahr_cap]值爲0 - 我得到一個除零錯誤。

我需要計數的數字重新除以0,因爲我不能將它們更改爲NULL值,所以我怎樣才能解決這個問題,因爲每次我得到除零錯誤查詢不計算月的數字有0,我必須將它們更改爲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值的月份。

請幫忙!

+1

你需要總和不算 –

+0

@ ZahiroMor所以我只是改變總數的計數?基本上是 - 基本上是 –

+1

。檢查我的答案 - 我改變了一些東西,如從數字值中刪除引號,並添加[standing_load]不爲null。嘗試它:) –

回答

2

爲了避免Divide by 0例外您查詢,在每種情況下,你做除法運算首先檢查是否分頻器是大於0的同時,爲什麼比較數值串?

也是你的第5個case缺失else部分。

見下面的代碼:

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] 

還有一個小意見,因爲我不知道這是否是您的解決方案可以接受,但我會改變計數sum和空爲0

+0

在這一個我沒有得到錯誤了,這是偉大的,但0值的計數是404,但細胞的總數是384,我應該只有10-20個單元在除數或除數中的值爲0? –

+0

這個CASE不應該以'THEN 1 ELSE NULL END'結束嗎? –

+0

@RichBenner CASE你在說什麼? –

0

試試這個

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] 
+0

它仍然給了我在這一個隊友0除錯錯誤 –