Skip to content

Running count of distinct buyers by first purchase month

MediumFull bankRunning totals, LAG and LEADCohorts and retention14 min2 hidden datasets

The task

The growth team charts how many different people have ever bought in the app. A user is a buyer once they have at least one purchase event. A user is counted once, in the month of their first purchase event, and never again.

Return one row for each calendar month in which at least one user made their first purchase:

  • month: the first day of the month
  • new_buyers: the number of users whose first purchase event was in that month
  • total_buyers: the number of distinct buyers up to and including that month, which is the sum of new_buyers for this and all earlier months

A month with only repeat purchases and no first purchases has no row. Sort by month.

Expected output

monthnew_buyerstotal_buyers
  • Sorted by month ascending.
  • Checked on the sample and on 2 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