mysql分組統計後將結果順序排列(union實現)

來源:互聯網
上載者:User

標籤:

  工作中用到統計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實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.