Cohort Retention Matrix From Month CTEs

Assign each user to the month of first activity, join follow-up months back, and render retention as conditional counts per months-since bucket.

SqlData Fetching

Code

snippet
-- retention matrix: cohort users by their first active month, then track how many stay active in each later month
CREATE TEMP TABLE events (
    user_id     integer NOT NULL,
    occurred_at date    NOT NULL
);

INSERT INTO events (user_id, occurred_at) VALUES
    (1, '2026-07-03'), (1, '2026-07-20'), (1, '2026-09-02'),
    (2, '2026-07-11'), (2, '2026-08-01'),
    (3, '2026-08-15'), (3, '2026-08-30');

WITH cohorts AS (
    -- first active month assigns the cohort
    SELECT user_id,
           date_trunc('month', min(occurred_at))::date AS cohort_month
    FROM events
    GROUP BY user_id
),
activity AS (
    -- every month each user was active (deduplicated)
    SELECT DISTINCT user_id,
           date_trunc('month', occurred_at)::date AS active_month
    FROM events
),
months AS (
    SELECT
        c.cohort_month,
        (extract(year FROM a.active_month) - extract(year FROM c.cohort_month)) * 12
            + extract(month FROM a.active_month)
            - extract(month FROM c.cohort_month) AS months_since,
        count(DISTINCT a.user_id) AS active_users
    FROM cohorts c
    JOIN activity a USING (user_id)
    GROUP BY c.cohort_month, months_since
)
SELECT
    cohort_month,
    count(*) FILTER (WHERE months_since = 0) AS size_m0,
    count(*) FILTER (WHERE months_since = 1) AS m1,
    count(*) FILTER (WHERE months_since = 2) AS m2
FROM months
GROUP BY cohort_month
ORDER BY cohort_month;

What the code does

Several statements share this file — SELECT / INSERT / CREATE, with events, a.active_month, c.cohort_month, cohorts and activity among the objects involved.

events exists only because this file creates it as a session-local temp table — nothing in advance, nothing left when the session closes.

Breaking the statements into their working parts: a CTE (WITH … AS) that names an intermediate result before the main query; one JOIN combining rows across tables; a WHERE clause narrowing which rows one statement affects; COUNT, MIN aggregation; a GROUP BY that collapses matching rows into one output row per group; an ORDER BY that fixes the output order; a DISTINCT qualifier that collapses duplicate rows; NOT NULL qualifiers on the required columns.

Applying it

The file is self-contained: it creates the session-local tables it uses (events), so run it as a whole against a scratch database.

Statements that matter

INSERT INTO events (user_id, occurred_at) VALUES
(1, '2026-07-03'), (1, '2026-07-20'), (1, '2026-09-02'),
(2, '2026-07-11'), (2, '2026-08-01'),
(3, '2026-08-15'), (3, '2026-08-30');
CREATE TEMP TABLE events (
user_id     integer NOT NULL,
occurred_at date    NOT NULL
);

Interactive Tools for This Topic

FAQ

What relations does the SQL touch?
`events`, `a.active_month`, `c.cohort_month`, `cohorts`, `activity` — joined via one JOIN clause.