mysql列转行以及年月分组实例
时间:2021-06-04 07:51:50|栏目: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
上一篇:B-Tree的性质介绍
栏 目:Mysql
下一篇:MySQL错误ERROR 2002 (HY000): Can't connect to local MySQL server through socket
本文标题:mysql列转行以及年月分组实例
本文地址:http://www.codeinn.net/misctech/134808.html