2013-02-12 79 views
0

詳細信息:SUM IF條件真回聲描述

lo means layout 
lo_act_id (activity id), lo_time (time spent) , up_time and down_time , lo_description , lo_client_id 

如果活動ID < 99意味着正常運行時間

如果活動ID> 99意味着停機時間,但我已經停機時間,我想請參閱它的說明/原因。

這是我使用它的偉大工程的查詢,但我不知道如何利用並顯示lo_description

SELECT * , 
    SUM(IF(`lo_act_id` > 99 , lo_time , 0)) AS down_time, 
    SUM(IF(`lo_act_id` < 99 , lo_time , 0 ,lo_desc)) AS up_time 
FROM tbl_Layout 
LEFT JOIN tbl_Client ON lo_client_id=client_code 
GROUP BY lo_client_id 

這是我得到

Client Spent Time Downtime 
1  216   3 
20  250   0 
16  10   16 
35  60   0 
60  30   0 

這是我想喜歡獲得:

Client Spend Time Downtime Note 
1   216   3   Replace Network Card 
20  250   0  
16  10   16   Replace Main Board 
            Monthly Maintenance 
            Hurricane affect facilities 
35  60   0 
60  30   0 

回答

0

使用CAST以擺脫「分鐘」和「分鐘」(假設此字段始終保留分鐘,而不是秒或小時)。

使用GROUP_CONCAT來粘貼說明。

SELECT 
    tbl_Client.*, 
    SUM(IF(`lo_act_id` > 99, CAST(lo_time AS UNSIGNED), 0)) AS down_time, 
    SUM(IF(`lo_act_id` < 99, CAST(lo_time AS UNSIGNED), 0)) AS up_time, 
    GROUP_CONCAT(IF(`lo_act_id` > 99, lo_description, NULL) SEPARATOR "\n") AS note 
FROM tbl_Layout 
LEFT JOIN tbl_Client ON lo_client_id=client_code 
GROUP BY lo_client_id