sql server 2008 - how to add the values as cummulative values in seperate column? -
this question has answer here:
- calculate running total in sql server 13 answers
col col1 col2 sum cumm 1 2 3 6 6 4 5 6 15 21 7 8 9 24 45 in above table result set need cumm column values , how query , can help?
try 1 -
query:
declare @temp table ( col1 int , col2 int , col3 int , col4 int ) insert @temp (col1, col2, col3, col4) values (1, 2, 3, 6), (4, 5, 6, 15), (7, 8, 9, 24) select col1 , col2 , col3 , col4 , sum_cumm = sum(col4) over( order col4 rows between unbounded preceding , current row) @temp output:
col1 col2 col3 col4 sum_cumm ----------- ----------- ----------- ----------- ----------- 1 2 3 6 6 4 5 6 15 21 7 8 9 24 45
Comments
Post a Comment