cae when分组统计

需求

数据如下:
在这里插入图片描述
想得到如下报表:

在这里插入图片描述

代码实现

create table test(usercode varchar, id int, oper_time date, duration int);

insert into test values('aaa', 1, date '2019-02-15', 12);
insert into test values('aaa', 2, date '2019-02-15', 8);
insert into test values('bbb', 3, date '2019-02-15', 0);
insert into test values('bbb', 4, date '2019-02-16', 15);

select * from test;




select oper_time, sum(case when usercode='aaa' then duration else 0 end) aaa ,sum(case when usercode='bbb' then duration else 0 end) bbb
from  test
group by oper_time
order by oper_time;
原文地址:https://www.cnblogs.com/yldf/p/11900012.html