下面小编就为大家带来一篇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列转行以及年月分组的示例代码分享的详细内容。