2011-03-19, 18:20
Hier ein kurzes Beispiel, wie man mit T-SQL eine kumulative Summe ausrechnet.
CREATE TABLE #test(datum DATE, VALUE INT)
INSERT INTO #test VALUES('2010-01-01',10)
INSERT INTO #test VALUES('2010-01-02',20)
INSERT INTO #test VALUES('2010-01-02',30)
SELECT t1.*, (SELECT SUM(VALUE) FROM #test t2 WHERE t2.datum<=t1.datum) [cumulative amount]
FROM #test t1
ORDER BY t1.datum
DROP TABLE #test |
create table #test(datum date, value int)
insert into #test values('2010-01-01',10)
insert into #test values('2010-01-02',20)
insert into #test values('2010-01-02',30)
select t1.*, (select sum(value) from #test t2 where t2.datum<=t1.datum) [cumulative amount]
from #test t1
order by t1.datum
drop table #test
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website