Oracle 查询日期连续的结果集
我有一张表(表名称T_Income),
字段和数据如下:
ID InputDate ItemName Income
1 2006-10-01 a 100
2 2006-10-01 aa 50
3 2006-10-01 aaa 150
4 2006-10-02 b 200
5 2006-10-02 bb 100
6 2006-10-02 bbb 300
7 2006-10-07 g 100
8 2006-10-07 gg 50
现在,我想查询一段时间以来每天的Income总量,如果某一天没有发生数据,也要去查询出来,只是数据为0而已。
例如,要查询从2006-10-01到2006-10-08的汇总,结果应该如下:
InputDate TotalIncome
2006-10-01 300
2006-10-02 600
2006-10-03 0
2006-10-04 0
2006-10-05 0
2006-10-06 0
2006-10-07 150
2006-10-08 0
高手们,一展你们的风采,帮帮小弟吧!
------解决方案--------------------我想这个只能通过存储过程用cursor来执行处理吧,单独的sql语句很难
------解决方案--------------------select b.InputDate, nvl(a.Income, 0)
from T_Income a,
(select min_date + rownum - 1 as InputDate
from (select min(InputDate) min_date, max(InputDate) max_date from T_Income)
connect by rownum <= max_date - min_date + 1) b
where b.InputDate = a.InputDate(+);
------解决方案--------------------select b.InputDate, sum(nvl(a.Income, 0))
from T_Income a,
(select min_date + rownum - 1 as InputDate
from (select min(InputDate) min_date, max(InputDate) max_date from T_Income)
connect by rownum <= max_date - min_date + 1) b
where b.InputDate = a.InputDate(+)
group by inputdate;