For reporting purposes we need to know how many users have signed up, but grouped into useful date range intervals (bins).
We also need to know the total number of users in the same date bins.
The following query works by creating a common table expression (CTE) where the
user.created_at column is transformed into a YEAR-MONTH string and a count of the number of users in that month “bin”.
Then a window operation (
OVER) is used generate a cumulative sum of the
WITH data as (
TO_CHAR(u.created_at,'yyyy-MM') AS yeardate,
FROM users u
GROUP BY yeardate
ORDER BY yeardate
SUM(count::int) OVER (
order by yeardate asc rows between unbounded preceding and current row
) AS cumulative
OVER incantation I copied from SO, so no idea if all those options are strictly required.
It would be easy to change the size of the date bins by changing how
yeardate is formatted.