Kumulative Summe mit T-SQL

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

Uwe

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