select * from u_md_dm.s_temp_import_d;
1、按照年和姓名来统计每个人每年的签到次数
select k.dates,sum(case when k.pro_d_name='张三' then k.times else 0 end) 张三,sum(case when k.pro_d_name='李四' then k.times else 0 end) 李四,sum(case when k.pro_d_name='王五' then k.times else 0 end) 王五
from (
select substr(pro_d_no,0,5) dates,pro_d_name,count(1) times from u_md_dm.s_temp_import_d group by substr(pro_d_no,0,5),pro_d_name) k
group by k.dates;
另:也可以用下面的语句实现:
with k as
(select substr(pro_d_no,0,5) dates,pro_d_name,count(1) times from u_md_dm.s_temp_import_d group by substr(pro_d_no,0,5),pro_d_name)
select
k.dates,sum(case when k.pro_d_name='张三' then k.times else 0 end) 张三,sum(case when k.pro_d_name='李四' then k.times else 0 end) 李四,sum(case when k.pro_d_name='王五' then k.times else 0 end) 王五
from k group by k.dates