Skip to content

Retention table for months 0 to 3

MediumFull bankCohorts and retentionDates and timeCTEs and subqueries20 min3 hidden datasets

The task

The growth team wants the raw numbers behind a retention triangle, in long format: one row per sign-up cohort and month number.

A user's cohort is the calendar month of users.signed_up_at. Month number 0 is the cohort's own month, 1 is the next calendar month, and so on up to 3. A user is retained in a month if they fired at least one event in events during that calendar month.

Rules:

  • Show every cohort with every month number from 0 to 3, even when nobody was retained (retained_users is then 0).
  • Leave out a cohort and month number when that calendar month is later than the month of the most recent event in events: the data does not cover it yet.
  • Count users, not events.

Return cohort_month (the first day of the cohort's month), month_number, cohort_size (users in the cohort, repeated on each row) and retained_users. Sort by cohort_month, then month_number.

Expected output

cohort_monthmonth_numbercohort_sizeretained_users
  • Sorted by cohort_month ascending, then month_number ascending.
  • Checked on the sample and on 3 hidden datasets. How grading works

Tables Mobile shopping app · click a name to insert it

users360 rows in the sample

One row per user. Key: user_id.

intUnique id.
timestampSign-up time (UTC).
textTwo-letter country code.
textios, android or web.
textorganic, paid, referral or partner. NULL when unknown.
events7,616 rows in the sample

One row per tracked event. Key: event_id.

bigintUnique id.
intWho fired it.
textapp_open, search, view_item, add_to_cart, begin_checkout, purchase or share.
timestampWhen (UTC).
textSession the event belongs to. NULL for a few events sent from the background.
numeric(10,2)Order value for purchase events, NULL otherwise.
experiment_assignments349 rows in the sample

Which variant each user saw in each experiment. Key: user_id, experiment.

intThe user.
textnew_onboarding or one_tap_checkout.
textcontrol or treatment.
timestampWhen the user was assigned (UTC).

Related questions

Loading the editor and PostgreSQL