標籤:
工作中用到統計12月份通話記錄,統計號碼撥打次數,但是問題出在一個號碼可以撥打多次,每次可能接通也可能不接通,如果用主叫號碼caller欄位group by分組後count(*)統計數目,這樣會導致不能看到統計數目中幾條是接通,幾條是未接通的,於是想到用union分是否接通各自統計,然後將統計結果按照號碼排序放到一起,具體實現如下
billid(通話記錄id),caller(主叫號碼),callerstarttime(撥打時間),callerResult(撥打結果0未接通 1接通)
sql實現語句如下:
SELECT bill_id,Caller,from_unixtime(CallerStartTime,‘%Y-%c-%d %h:%i:%s‘) as date,CallResult,count(*) as num FROM `sup_tel_record` where CallerStartTime > 1417363200 and CallerStartTime < 1420041600 and CallResult = 0 group by Caller unionSELECT bill_id,Caller,from_unixtime(CallerStartTime,‘%Y-%c-%d %h:%i:%s‘) as date,CallResult,count(*) as num FROM `sup_tel_record` where CallerStartTime > 1417363200 and CallerStartTime < 1420041600 and CallResult = 1 group by Caller order by date asc
輸出結果如下:
注意使用union進行sql語句串連時,第一條sql語句不能使用order by排序,否則會出現
mysql錯誤 1221 Incorrect usage of union and order by
mysql分組統計後將結果順序排列(union實現)