有這樣一個(gè)問(wèn)題,如何按日期累計(jì)值
如下表所示:
日期 數(shù)值
2009-02-05 2.4
2009-02-06 2
2009-02-07 5
2009-02-08 3.5
2009-02-05 2.4
2009-02-06 2
2009-02-07 5
2009-02-08 3.5
要實(shí)現(xiàn)如下結(jié)果:
日期 數(shù)值 累計(jì)
2009-02-05 2.4 2.4
2009-02-06 2 4.4
2009-02-07 5 9.4
2009-02-08 3.5 12.9
使用以下sql語(yǔ)句即可實(shí)現(xiàn):
--累計(jì)計(jì)算
select aa.日期,aa.數(shù)值,sum(lj.數(shù)值) as 累計(jì)
from 累計(jì) lj inner join
(select *
from 累計(jì)) aa on lj.日期<=aa.日期
where (aa.日期<='2009-02-08') --日期可變?yōu)闀r(shí)間段
group by aa.日期,aa.數(shù)值
order by aa.日期