mysql列转行以及年月分组的示例代码分享
发表时间:2023-09-04 来源:明辉站整理相关软件相关文章人气:
[摘要]下面小编就为大家带来一篇mysql列转行以及年月分组实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧如下所示:SELECT count(DISTINCT(a.rect_id)) zcount, a.job_dept, DATE_FORMAT(submit_date...
下面小编就为大家带来一篇
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列转行以及年月分组的示例代码分享的详细内容,更多请关注php中文网其它相关文章!
学习教程快速掌握从入门到精通的SQL知识。