MySQL⽇期列转⾏list_mysql列转⾏以及年⽉分组实例如下所⽰:
SELECT count(_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,
group by的用法及原理详解
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(_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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论