我已經搜索了似乎相關的問題,但仍無法解決問題。當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'
編輯。
的修補程序表信息:
我的預期輸出是由機器,他們的補丁狀態列出,而是完全使用補丁的狀態不會顯示:
感謝任何協助。
問候彼得。
考慮提供樣本數據集,並用正確的表定義,並添加你想要的結果集的 – 2014-10-02 05:34:04
可能重複[如何包括「零」 /「0」 COUNT彙總結果?] (http://stackoverflow.com/questions/14793057/how-to-include-zero-0-results-in-count-aggregate) – Phil 2014-10-02 05:54:49
我認爲這是可以預料的,因爲分組正在從另一個表格的字段上完成。哪張桌子總是有數據? – RMK 2014-10-02 06:19:03