首页 > Linux操作系统 > Linux操作系统 > sql实现年/月度数据累计
基础数据
SQL> select appliedtime, data
2 from table1
3 where appliedtime between to_date('2009-12-26','yyyy/mm/dd hh24:mi:ss') and to_date('2010-01-05','yyyy/mm/dd hh24:mi:ss')
3 order by appliedtime;
APPLIEDTIME DATA
-------------- ----------
26-12月-09 1468.386
27-12月-09 1038.037
28-12月-09 1047.502
29-12月-09 1289.621
30-12月-09 718.156
31-12月-09 725.31
01-1月 -10 752.843
02-1月 -10 723.482
03-1月 -10 784.363
04-1月 -10 774.571
05-1月 -10 796.943
| 月度累计 |
SQL> select a.appliedtime,a.data,sum(b.data) monthData
2 from table1 a,table1 b
3 where b.appliedtime <= a.appliedtime
4 and extract(year from b.appliedtime)=extract(year from a.appliedtime)
5 and extract(month from b.appliedtime)=extract(month from a.appliedtime)
6 and a.appliedtime between to_date('2009-12-26','yyyy/mm/dd hh24:mi:ss') and to_date('201
0-01-05','yyyy/mm/dd hh24:mi:ss')
7 and b.appliedtime between to_date('2009-12-26','yyyy/mm/dd hh24:mi:ss') and to_date('201
0-01-05','yyyy/mm/dd hh24:mi:ss')
8 group by a.appliedtime,a.data
9 order by a.appliedtime;
APPLIEDTIME DATA MONTHDATA
-------------- - --------- ----------
26-12月-09 1468.386 1468.386
27-12月-09 1038.037 2506.423
28-12月-09 1047.502 3553.925
29-12月-09 1289.621 4843.546
30-12月-09 718.156 5561.702
31-12月-09 725.31 6287.012
01-1月 -10 752.843 752.843
02-1月 -10 723.482 1476.325
03-1月 -10 784.363 2260.688
04-1月 -10 774.571 3035.259
05-1月 -10 796.943 3832.202
年度累计只需将月度累计中第5行sql注释就可以了
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23278472/viewspace-626955/ ,如需转载,请注明出处,否则将追究法律责任。