pawel7318 pawel7318 - 9 months ago 77
SQL Question

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.

Answer Source

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;