New user signups by month with running total

Problem

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.

Solution

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 count

WITH data as (
    SELECT 
        TO_CHAR(u.created_at,'yyyy-MM') AS yeardate, 
        COUNT(*) 
    FROM users u
    GROUP BY yeardate 
    ORDER BY yeardate
)
SELECT 
    yeardate,
    count,
    SUM(count::int) OVER (
        order by yeardate asc rows between unbounded preceding and current row
    ) AS cumulative
FROM data

The 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.

We’ve got some similar ones in this topic if you’d like to test them out and see how they compare:

1 Like

Oh nice! Thanks.

I thought I’d searched, clearly not well enough or insufficient secret-sauce.

That would have saved me some time … sigh

2 Likes