pawel7318
pawel7318

Reputation: 3573

group by with sum

Simplifying I have something like this:

select register_date,count(*)
from USER
group by register_date;

and result is:

2016-01-20  10
2016-01-21  5
2016-01-22  8

This shows me that at first day 10 users registered than 5 and 8 next days. I need to have a sum of registered users:

2016-01-20  10
2016-01-21  15 (10+5)
2016-01-22  23 (10+5+8)

Any ideas how to get it ? Thank you in advance.

Upvotes: 0

Views: 65

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269493

Use a cumulative sum. You can do this with an aggregation query, even:

select register_date, count(*),
       sum(count(*)) over (order by register_date) as running_count
from USER
group by register_date
order by register_date;

Upvotes: 4

Related Questions