Virender Dubey
Virender Dubey

Reputation: 206

How can i get the aggregated data from SQL Group by

suppose i have a table-

HOUR  Value
 0   10
 1    5
 2   12

and i want the result as HOUR ---> Sum of Value

0    10
1    15
2    27

Upvotes: 0

Views: 37

Answers (2)

JBond
JBond

Reputation: 3242

As I had misread the fact it was a running total of consecutive numbers. I've updated my answer to be an alternative to the answer @jarlh gave by using JOIN instead.

SELECT T.Hour, T.value, SUM(TT.Value) S
FROM   
TimeTable T
CROSS JOIN TimeTable TT
WHERE TT.Hour <= T.Hour
GROUP BY T.Hour, T.Value
ORDER BY 1

Upvotes: 2

jarlh
jarlh

Reputation: 44786

Core ANSI SQL answer, use a correlated sub-query to sum up values:

select hour, (select sum(value) from tablename t2
              where t2.hour <= t1.hour)
from tablename t1

Upvotes: 3

Related Questions