如何在日期字段中使用案例

时间:2018-06-12 10:53:05

标签: mysql sql

我希望在一个字段中调用numbernumber,并使用主叫号码

进行月份明智的记录
SELECT calling_number, 
       ( CASE 
           WHEN Date(start_time) BETWEEN '2018-03-01' AND '2018-03-31' THEN 
           Sec_to_time(Sum(duration)) 
           ELSE NULL 
         end ) AS March, 
       ( CASE 
           WHEN Date(start_time) BETWEEN '2018-04-01' AND '2018-04-31' THEN 
           Sec_to_time(Sum(duration)) 
           ELSE NULL 
         end ) AS April, 
       ( CASE 
           WHEN Date (start_time) BETWEEN '2018-05-01' AND '2018-05-31' THEN 
           Sec_to_time(Sum(duration)) 
           ELSE NULL 
         end ) AS May 
FROM   telephone_report 
GROUP  BY calling_number 
ORDER  BY duration 

我想这样结果

Calling_number    April       May        June
1024              10:20:20    05:10:05   05:00:02

1 个答案:

答案 0 :(得分:0)

我猜你需要一个持续时间的条件总和

SELECT calling_number, 
    SEC_TO_TIME(SUM(CASE  WHEN DATE(start_time) BETWEEN '2018-03-01' AND '2018-03-31' THEN duration ELSE 0 END )) March,
    SEC_TO_TIME(SUM(CASE  WHEN DATE(start_time) BETWEEN '2018-04-01' AND '2018-04-30' THEN duration ELSE 0 END )) April,
    SEC_TO_TIME(SUM(CASE  WHEN DATE(start_time) BETWEEN '2018-05-01' AND '2018-05-31' THEN duration ELSE 0 END )) May
FROM   telephone_report 
GROUP  BY calling_number