Cumulative sum with group by

0

Please login or register to vote for this query.

(click on this box to dismiss)

Russian Language

Q&A for students, teachers, and linguists wanting to discuss the finer points of the Russian language

DECLARE @t TABLE(visite INT, mese INT, anno INT);

INSERT INTO @t VALUES
(6 ,         6, 2017),
(7 ,         7, 2017),
(8  ,        8, 2017),
(9   ,       9, 2017),
(11   ,       10, 2017),
(14   ,       11, 2017),
(7   ,       12, 2017),
(4   ,       1, 2018),
(0   ,       2, 2018),
(1   ,       3, 2018);
;

WITH cteRanked AS (
   SELECT visite, mese, anno, ROW_NUMBER() OVER(ORDER BY anno, mese) rownum
   FROM @t
   ) 

SELECT (SELECT SUM(visite) FROM cteRanked c2 WHERE c2.rownum <= c1.rownum) 
AS visite, mese, anno
FROM cteRanked c1;

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results