Skip to content
RED SAG Skills
hard20 pts · first answer countschurn-preventionsqlmrr-movements

SQL: classify MRR changes with LAG

This query labels each customer's monthly MRR change. For a customer whose MRR goes from 500 to 300, which label is produced?

SELECT customer_id, month, mrr,
  CASE
    WHEN prev IS NULL OR prev = 0 THEN 'new_or_reactivated'
    WHEN mrr = 0 THEN 'churn'
    WHEN mrr > prev THEN 'expansion'
    WHEN mrr < prev THEN 'contraction'
    ELSE 'flat'
  END AS movement
FROM (
  SELECT *, LAG(mrr) OVER (PARTITION BY customer_id ORDER BY month) AS prev
  FROM mrr_monthly
) t;
Choose one answer

Discussion 0

Join free to post in the discussion.

Related questions