2014-10-02 34 views
0

我已經搜索了似乎相關的問題,但仍無法解決問題。當count = 0時,按中斷計數組

SELECT c.name AS 'Computer', COUNT(h.hotfixid) AS '# Missing', 
CASE 
    WHEN COUNT(h.hotfixid) ='0' THEN 'Fully Patched' 
    WHEN COUNT(h.hotfixid) BETWEEN 1 AND 4 THEN '1 - 4 Missing' 
    WHEN COUNT(h.hotfixid) BETWEEN 5 AND 12 THEN '5 - 12 Missing' 
    WHEN COUNT(h.hotfixid) >12 THEN 'Attention > 12 Missing' 
    END AS 'Status', 
c.os AS 'Operating System' 
FROM hotfix h 
LEFT JOIN computers c ON h.computerid=c.computerid 
LEFT JOIN clients cl ON c.clientid=cl.clientid 
WHERE h.Installed='0' AND h.Approved='1' AND c.clientid = '229' 
GROUP BY c.name 

問題是,上述不會返回任何有0或沒有記錄的東西。 GROUP BY打破它。

該查詢返回我的預期的結果,但是當我添加GROUP BY它沒有結果在全部返還。

SELECT 
CASE 
    WHEN COUNT(hotfixid) ='0' THEN 'Fully Patched' 
    WHEN COUNT(hotfixid) BETWEEN 1 AND 4 THEN '1 - 4 Missing' 
    WHEN COUNT(hotfixid) BETWEEN 5 AND 12 THEN '5 - 12 Missing' 
    WHEN COUNT(hotfixid) >12 THEN 'Attention > 12 Missing' 
    END AS 'Status' 
FROM hotfix 
WHERE computerid = '8176' AND hotfix.`approved` = '1' AND hotfix.`installed` = '0' 

該查詢返回一個0,所以我知道我得到0,而不是 'NULL'

SELECT computerid, 
    COUNT(hotfixid) 
    FROM hotfix 
    WHERE computerid = '8176' AND hotfix.`approved` = '1' AND hotfix.`installed` = '0' 

編輯。

的修補程序表信息:

enter image description here

我的預期輸出是由機器,他們的補丁狀態列出,而是完全使用補丁的狀態不會顯示:

MySQL Yog output

感謝任何協助。

問候彼得。

+1

考慮提供樣本數據集,並用正確的表定義,並添加你想要的結果集的 – 2014-10-02 05:34:04

+0

可能重複[如何包括「零」 /「0」 COUNT彙總結果?] (http://stackoverflow.com/questions/14793057/how-to-include-zero-0-results-in-count-aggregate) – Phil 2014-10-02 05:54:49

+0

我認爲這是可以預料的,因爲分組正在從另一個表格的字段上完成。哪張桌子總是有數據? – RMK 2014-10-02 06:19:03

回答

0

更有意義有computers左聯接hotfix。而c.os中缺少group by.

SELECT c.name AS 'Computer', COUNT(h.hotfixid) AS '# Missing', 
CASE 
    WHEN COUNT(h.hotfixid) ='0' THEN 'Fully Patched' 
    WHEN COUNT(h.hotfixid) BETWEEN 1 AND 4 THEN '1 - 4 Missing' 
    WHEN COUNT(h.hotfixid) BETWEEN 5 AND 12 THEN '5 - 12 Missing' 
    WHEN COUNT(h.hotfixid) >12 THEN 'Attention > 12 Missing' 
    END AS 'Status', 
c.os AS 'Operating System' 
FROM computers c 
LEFT OUTER JOIN hotfix h ON c.computerid=h.computerid AND h.Installed='0' AND h.Approved='1' 
LEFT OUTER JOIN clients cl ON c.clientid=cl.clientid AND c.clientid = '229' 
GROUP BY c.name, c.os 
+0

Hi @chianh,謝謝。我完全按照你所示的方式運行,結果沒有改變。仍然沒有去。 – PeterM 2014-10-02 06:48:30

+0

更新後加入條件。如果可以,請給我一個數據樣本 – anhlc 2014-10-02 06:52:42

+0

是的,你現在明白了@chianh謝謝! – PeterM 2014-10-02 07:16:43