mysql列轉行以及年月分組實例。本站提示廣大學習愛好者:(mysql列轉行以及年月分組實例)文章只能為提供參考,不一定能成為您想要的結果。以下是mysql列轉行以及年月分組實例正文
如下所示:
SELECT count(DISTINCT(a.rect_id)) zcount, a.job_dept, DATE_FORMAT(submit_date, '%Y-%m') zsubmit_date FROM 表名 a WHERE a.statu = 3 AND a.rstatu = 2 AND a.job_dept IN ('19', '20', '21') GROUP BY a.job_dept, DATE_FORMAT(submit_date, '%Y-%m')
其中關鍵在於DATE_FORMAT(submit_date, '%Y-%m')對時間年月進行了分組排序
SELECT zsubmit_date, MAX(CASE WHEN job_dept = '19' THEN zcount ELSE 0 END ) 19zcount, MAX(CASE WHEN job_dept = '20' THEN zcount ELSE 0 END ) 20zcount, MAX(CASE WHEN job_dept = '21' THEN zcount ELSE 0 END ) 21zcount FROM ( SELECT count(DISTINCT(a.rect_id)) zcount, a.job_dept, DATE_FORMAT(submit_date, '%Y-%m') zsubmit_date FROM 表名 a WHERE a.statu = 3 AND a.rstatu = 2 AND a.job_dept IN ('19', '20', '21') GROUP BY a.job_dept, DATE_FORMAT(submit_date, '%Y-%m') ) q GROUP BY zsubmit_date
以上這篇mysql列轉行以及年月分組實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。