2012-05-26 91 views
0

我想貯槽兩個或多個計數值[樞] 我的查詢是如下:如何在MySQL查詢中計算兩個或更多COUNT值?

SELECT 
    client_name.client_name , 
    escort , 
    count(case mission_status_reason when '4' then mission_status_reason end) as awaiting_upload_at_origin, 
    count(case mission_status_reason when '6' then mission_status_reason end) as awaiting_military_escort, 
    count(case mission_status_reason when '3' then mission_status_reason end) as enrouted_to_destination, 
    count(case mission_status_reason when '9' then mission_status_reason end) as awaiting_download, 
    (awaiting_military_escort +enrouted_to_destination+ awaiting_download) as TOTAL 
FROM usc_tmr 
    LEFT JOIN client_name on client_name.id = usc_tmr.client_name 
WHERE escort='usg' 
    and mission_status_ops IN ('1','4','5','6') 
GROUP BY client_name 

我的問題是計算總。你們能幫忙嗎?

+2

你試過用SUM()嗎? –

+0

Yap。不工作。 – marifrahman

+0

我得到的全部是未知列錯誤 – marifrahman

回答

1
SELECT client_name.client_name, 
     escort, 
     SUM(mission_status_reason = 4)  AS awaiting_upload_at_origin, 
     SUM(mission_status_reason = 6)  AS awaiting_military_escort, 
     SUM(mission_status_reason = 3)  AS enrouted_to_destination, 
     SUM(mission_status_reason = 9)  AS awaiting_download, 
     SUM(mission_status_reason IN (3,6,9)) AS TOTAL 
FROM  usc_tmr LEFT JOIN client_name ON client_name.id = usc_tmr.client_name 
WHERE escort = 'usg' AND mission_status_ops IN (1,4,5,6) 
GROUP BY client_name 
+0

很酷。感謝:D – marifrahman