-2

I have a table like below :

ID VALUE
1 200
2 300
3 100
4 50
5 1000
6 20

I want a row level sum for each row. Like for ID 1,it should be the sum of VALUEs from ID 1 to 6 , for ID 2 it should be 2 to 6 and like this .

My desired output should look like this :

ID VALUE AGG_VAL
1 200 1670
2 300 1470
3 100 1170
4 50 1070
5 1000 1020
6 20 20
Thom A
  • 88,727
  • 11
  • 45
  • 75
Sayandip Ghatak
  • 191
  • 2
  • 14

1 Answers1

3

You'll need to use OVER function:

SELECT ID, 
       VALUE, 
       SUM(VALUE) OVER (ORDER BY ID DESC) AS AGG_VAL
FROM TableName
ORDER BY ID
Neil W
  • 7,670
  • 3
  • 28
  • 41