有如下数据:
日期 姓名 金额
2008-1-12 sun 100
2008-1-12 张 100
2008-1-13 sun 100
2008-1-19 张 300
2008-2-12 sun 120
2008-2-19 张 300
2008-3-12 sun 130
得到的查询结果:
姓名 2008-01 2008-02 2008-03
sun 200 120 130
张 400 300
0
我的答案(非最佳答案.)
select t1.username "姓名","2009-09","2009-10","2009-11"
from (select username,sum(money) as "2009-09" from SCOTT.MY where datetime between to_date('2009-09-01','yyyy-mm-dd') and to_date('2009-09-30','yyyy-mm-dd') group by username) t1,
(select username,sum(money) as "2009-10" from SCOTT.MY where datetime between to_date('2009-10-01','yyyy-mm-dd') and to_date('2009-10-30','yyyy-mm-dd') group by username) t2,
(select username,sum(money) as "2009-11" from SCOTT.MY where datetime between to_date('2009-11-01','yyyy-mm-dd') and to_date('2009-11-30','yyyy-mm-dd') group by username) t3
where t1.username = t2.username and t2.username = t3.username;
本文提供了一个SQL分组查询的实际案例,展示了如何通过多个子查询来汇总不同月份的数据,并按用户名进行分组,以便更好地理解和应用SQL聚合函数。

被折叠的 条评论
为什么被折叠?



