Revenue to date per merchant

running totals

Revenue to date per merchant

Shopify SQL Interview Question

Shopify spends to acquire a merchant and earns it back one month at a time. The question that sets the marketing budget is how many months that payback takes. Answering it needs the money a merchant has paid so far at each month of their life, not their lifetime total.

Return the merchant_id, the active_month, that month's subscription revenue as mrr, and everything the merchant has paid up to and including that month as revenue_to_date. Sort by merchant_id and then by active_month.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist

shopify_subscriptionsTable26 rows

Column NameType
record_idBIGINT
merchant_idVARCHAR
active_monthDATE
planVARCHAR
mrrDOUBLE

shopify_subscriptionsExample Input

record_idmerchant_idactive_monthplanmrr
1101M012024-01-01Basic29
1102M012024-02-01Basic29
1103M012024-03-01Shopify79
1104M012024-06-01Shopify79
1105M012024-07-01Advanced299
1106M012024-08-01Advanced299
1119M042024-01-01Advanced299
1120M042024-02-01Advanced299
1121M042024-03-01Advanced299
1122M042024-04-01Advanced299

Example Output

merchant_idactive_monthmrrrevenue_to_date
M012024-01-012929
M012024-02-012958
M012024-03-0179137
M012024-06-0179216
M012024-07-01299515
M012024-08-01299814
M042024-01-01299299
M042024-02-01299598
M042024-03-01299897
M042024-04-012991196

Explanation

M01 pays 29 in its first month, so the running figure starts at 29, reaches 58 after the second month and 137 after the third. The two months it was gone are simply absent, and the total carries straight across the gap.

The example above is a small slice of the data. Your query runs against the full tables.