Tuesday, June 15, 2010

Running total

Query:

with data as
(
select * from 
(values 
  (25),(3),(4),(7),(15),(24),(20),(27), (11)
) as x(y)
)
select y, sum(y) over(order by y) from data

Sample output:
 y  | sum 
----+-----
  3 |   3
  4 |   7
  7 |  14
 11 |  25
 15 |  40
 20 |  60
 24 |  84
 25 | 109
 27 | 136
(9 rows)

No comments:

Post a Comment