SQL Dojo
Metric

MRR: monthly recurring revenue and its movements

The monthly recurring revenue on the first of each month, annual plans divided by 12, and the new, expansion, contraction and churn that move it.

How much recurring revenue do the running subscriptions bring in each month? MRR is measured on a day, the first of the month: every subscription active that day adds its monthly price, an annual plan its price divided by 12. ended_on is the first day a subscription no longer runs.

WITH months AS (
  SELECT CAST(d AS DATE) AS month
  FROM generate_series(DATE '2025-01-01', DATE '2025-12-01', INTERVAL '1 month') AS t(d)
)
SELECT m.month,
       SUM(CASE WHEN s.billing_period = 'annual' THEN s.price / 12 ELSE s.price END) AS mrr
FROM months m
JOIN subscriptions s
  ON s.started_on <= m.month
 AND COALESCE(s.ended_on, DATE '9999-12-31') > m.month
GROUP BY m.month
ORDER BY m.month;

The movements say why it moved: compare each account's MRR on the 1st with the 1st of the next month. From 0 is new, up is expansion, down but above 0 contraction, down to 0 churn; opening + the four = closing.

The trap. MRR is not cash: built from invoices, an annual plan counts twelve months in the month it is billed. It is a stock: a quarter's MRR is its last month's, not the sum of three. And for the movements, give every account a row each month, at 0 without a subscription, or its churn disappears.

On your warehouse. generate_series is Postgres's: BigQuery writes UNNEST(GENERATE_DATE_ARRAY('2025-01-01', '2025-12-01', INTERVAL 1 MONTH)), Databricks explode(sequence(DATE '2025-01-01', DATE '2025-12-01', INTERVAL 1 MONTH)), and Snowflake builds the months with GENERATOR or reads a calendar table.

WITH subscriptions(account_id, billing_period, price, started_on, ended_on) AS (VALUES
  (1, 'monthly', 100.00, DATE '2025-01-01', DATE '2025-02-15'),
  (1, 'monthly', 150.00, DATE '2025-02-15', NULL),
  (2, 'annual', 1200.00, DATE '2024-03-20', DATE '2025-03-20'),
  (3, 'monthly', 50.00, DATE '2025-01-10', DATE '2025-03-10'),
  (3, 'monthly', 30.00, DATE '2025-03-10', NULL),
  (4, 'annual', 960.00, DATE '2025-02-01', NULL)
),
months AS (
  SELECT CAST(d AS DATE) AS month
  FROM generate_series(DATE '2025-01-01', DATE '2025-04-01', INTERVAL 1 MONTH) AS t(d)
)
SELECT m.month,
       SUM(CASE WHEN s.billing_period = 'annual' THEN s.price / 12 ELSE s.price END) AS mrr
FROM months m
JOIN subscriptions s
  ON s.started_on <= m.month
 AND COALESCE(s.ended_on, DATE '9999-12-31') > m.month
GROUP BY m.month
ORDER BY m.month;
monthmrr
2025-01-01200.0
2025-02-01330.0
2025-03-01380.0
2025-04-01260.0
Try it in Randori
-- The same accounts, one row per account and 1st of the month (0 without a subscription)
WITH account_mrr(account_id, month, mrr) AS (VALUES
  (1, DATE '2025-01-01', 100), (1, DATE '2025-02-01', 100), (1, DATE '2025-03-01', 150), (1, DATE '2025-04-01', 150),
  (2, DATE '2025-01-01', 100), (2, DATE '2025-02-01', 100), (2, DATE '2025-03-01', 100), (2, DATE '2025-04-01', 0),
  (3, DATE '2025-01-01', 0),   (3, DATE '2025-02-01', 50),  (3, DATE '2025-03-01', 50),  (3, DATE '2025-04-01', 30),
  (4, DATE '2025-01-01', 0),   (4, DATE '2025-02-01', 80),  (4, DATE '2025-03-01', 80),  (4, DATE '2025-04-01', 80)
),
moves AS (
  SELECT month, mrr AS opening,
         LEAD(mrr) OVER (PARTITION BY account_id ORDER BY month) AS closing
  FROM account_mrr
)
SELECT month,
       SUM(opening) AS opening_mrr,
       SUM(CASE WHEN opening = 0 AND closing > 0 THEN closing ELSE 0 END) AS new_mrr,
       SUM(CASE WHEN opening > 0 AND closing > opening THEN closing - opening ELSE 0 END) AS expansion_mrr,
       SUM(CASE WHEN closing > 0 AND closing < opening THEN closing - opening ELSE 0 END) AS contraction_mrr,
       SUM(CASE WHEN opening > 0 AND closing = 0 THEN -opening ELSE 0 END) AS churned_mrr,
       SUM(closing) AS closing_mrr
FROM moves
WHERE closing IS NOT NULL
GROUP BY month
ORDER BY month;
monthopening_mrrnew_mrrexpansion_mrrcontraction_mrrchurned_mrrclosing_mrr
2025-01-01200130000330
2025-02-0133005000380
2025-03-0138000-20-100260
Try it in Randori