mysql列转行以及年月分组实例
如下所示:
SELECTcount(DISTINCT(a.rect_id))zcount,a.job_dept, DATE_FORMAT(submit_date,'%Y-%m')zsubmit_date FROM 表名a WHERE a.statu=3 ANDa.rstatu=2 ANDa.job_deptIN('19','20','21') GROUPBY a.job_dept, DATE_FORMAT(submit_date,'%Y-%m')
其中关键在于DATE_FORMAT(submit_date,'%Y-%m')对时间年月进行了分组排序
SELECT zsubmit_date, MAX(CASEWHENjob_dept='19'THENzcountELSE0END)19zcount, MAX(CASEWHENjob_dept='20'THENzcountELSE0END)20zcount, MAX(CASEWHENjob_dept='21'THENzcountELSE0END)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 ANDa.rstatu=2 ANDa.job_deptIN('19','20','21') GROUPBY a.job_dept, DATE_FORMAT(submit_date,'%Y-%m') )qGROUPBY zsubmit_date
以上这篇mysql列转行以及年月分组实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。