2012-11-12 253 views
0

我想指望C和M 我開始與此查詢的警報數和用戶類型類型=處理警報的數量它給了我主要和關鍵警報數量正確的,但我「M卡住處理集團通過查詢SQL

SELECT 
    TYPE, 
    user_name, 
    COUNT(type_alertes) AS nb, 
    id_user, 
    SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique, 
    SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur 
FROM stat_alert 
LEFT OUTER JOIN user_qdf ON user_qdf.`id` = id_user 
WHERE 
    user_qdf.`id`=id_user 
    AND TYPE IN ('M', 'C') 
GROUP BY id_user 

警報的人數誰可以幫我

+0

由 – Kashif

+0

相同的結果 – user1794019

+0

你爲什麼要加入同一個表多次添加型,USER_NAME組?我懷疑你的加入。表user_qdf和id_user之間的連接字段在哪裏 – polin

回答

0

如果你想所有警報的數量,從數M的的C和編號,然後

SELECT 
    user_name, 
    COUNT(type_alertes) AS nb, 
    id_user, 
    SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique, 
    SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur 
FROM stat_alert 
JOIN user_qdf ON user_qdf.`id` = id_user 
GROUP BY id_user, user_name 

應該這樣做。 我假設user_name來自user_qdf表,其他來自stat_alert並且id_user不爲null。

編輯:對於alertes

的總和
SELECT 
    user_name, 
    COUNT(type_alertes) AS nb, 
    id_user, 
    SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique, 
    SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur 
    SUM(CASE WHEN TYPE ='M' OR TYPE = 'C' THEN 1 ELSE 0 END) AS both_c_and_m 
FROM stat_alert 
JOIN user_qdf ON user_qdf.`id` = id_user 
GROUP BY id_user, user_name 

EDIT2:

SELECT 
    user_name, 
    COUNT(type_alertes) AS nb, 
    SUM(CASE WHEN TYPE ='C' THEN type_alertes ELSE 0 END) AS critique_alerts, 
    SUM(CASE WHEN TYPE ='M' THEN type_alertes ELSE 0 END) AS majeur_alertes 
    id_user, 
    SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique, 
    SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur 
FROM stat_alert 
JOIN user_qdf ON user_qdf.`id` = id_user 
WHERE TYPE in ('C','M') 
GROUP BY id_user, user_name 

是你想要的嗎?

+0

我卡住了,如果類型= C,如果處理警報的數量(colmun type_alertes) type = M – user1794019

+0

我不完全明白你想要什麼。你想爲每個type_alertes計算C和M嗎? TYPE是一個user_qdf列,type_alertes是stat_alert列? – sufleR

+0

我想爲我的主要關鍵字鍵入警報,並計算類型= C的警報草稿數,同樣類型= M – user1794019