The situation
Finance tracks how each subscription plan's revenue changes from one month to the next. The source table only has a row for a plan in a month when the plan earned something.
The current report shows the Standard plan jumping sharply in December. It did not. The report compared December with October, because November has no row.
Input tables
monthly_revenue
One row per plan and month in which the plan had revenue. Months with no revenue have no row.
| Column | Type | Description |
|---|---|---|
plan_name |
STRING | The subscription plan. |
revenue_month |
DATE | The first day of the month. |
revenue |
DECIMAL(12,2) | Revenue of the plan in that month. |
Requirement
Return every row of monthly_revenue with the previous month's revenue next to it.
prev_month_revenueis the plan's revenue in the calendar month immediately beforerevenue_month. January 2025 is compared with December 2024.- If the plan has no row for that previous month,
prev_month_revenueis NULL. Never compare with a month further back. revenue_changeisrevenueminusprev_month_revenue, and NULL whenprev_month_revenueis NULL.
Expected output
One row per row of monthly_revenue.
| Column | Type | Description |
|---|---|---|
plan_name |
STRING | |
revenue_month |
DATE | |
revenue |
DECIMAL(12,2) | |
prev_month_revenue |
DECIMAL(12,2) | NULL if the plan has no row for the previous month. |
revenue_change |
DECIMAL(13,2) | NULL if prev_month_revenue is NULL. |
Solve it in the browser
This editor runs DuckDB inside your browser. Nothing is installed and nothing is sent to a server. DuckDB's SQL is close to Spark SQL but not identical. The Colab notebook runs real Spark.
Your result appears here. Your draft is kept in this browser.
Explanation
Show explanationGives the answer away. Try the problem first.
The approach
LAG(revenue) returns the value from the previous row of the window. Ordered by
month, the previous row is the previous month only if no month is missing.
The Standard plan has rows for October and December but not November. For the December
row, LAG returns October's revenue. The number is real, the label "previous month"
is not.
The fix is to check what the previous row actually is before using it:
CASE
WHEN LAG(revenue_month) OVER (PARTITION BY plan_name ORDER BY revenue_month)
= revenue_month - INTERVAL 1 MONTH
THEN LAG(revenue) OVER (PARTITION BY plan_name ORDER BY revenue_month)
END AS prev_month_revenue
If the previous row is exactly one month earlier, use its revenue. Otherwise the
CASE has no matching branch and returns NULL.
revenue_change then needs no special handling. A number minus NULL is NULL, which is
what the requirement asks for.
The table has 64 rows for 6 plans. 10 rows have no previous month: the first row of each plan, and 4 rows that follow a gap.
An equally good solution
A self-join says the same thing more directly:
SELECT a.plan_name, a.revenue_month, a.revenue, b.revenue AS prev_month_revenue, ...
FROM monthly_revenue a
LEFT JOIN monthly_revenue b
ON b.plan_name = a.plan_name
AND b.revenue_month = a.revenue_month - INTERVAL 1 MONTH
It looks up the row for the previous month and finds nothing if it does not exist. It must be a left join, or every row without a previous month disappears.
Common wrong answers
Plain LAG(revenue). Row count is right, and 4 rows are wrong: the ones after a
gap are compared with a month two or three months back. Sixty rows are correct, which
is why this survives a casual check. Whenever a report says "previous period", ask
whether the data is guaranteed to have every period.
Joining on the month number. MONTH(b.revenue_month) = MONTH(a.revenue_month) - 1
with the same year works from February to December and fails every January: month 1
minus 1 is 0, and no month has number 0. December of the previous year is never
found. Use date arithmetic, not arithmetic on parts of a date.
Inner self-join. Returns 54 rows. The 10 rows without a previous month are dropped, including the first month of every plan.
COALESCE(prev_month_revenue, 0) in the subtraction. Makes revenue_change equal
to the full revenue wherever there is no previous month. A new plan appears to have
grown from nothing by its entire revenue, and a plan with a missing month appears to
have had a huge jump. Unknown is not zero.
An alternative: fill the gaps first
If the report needs a row for the missing months too (showing revenue 0), build the
complete list of plan and month pairs first, as in problem 15, and then plain LAG
is correct because no row is missing any more. Which approach is right depends on
whether "no row" means "zero revenue" or "we do not know".
Edge cases in the data
| Case | Plan | What it catches |
|---|---|---|
| One month missing in the middle | Standard (November) | Plain LAG. |
| January missing | Premium | Plain LAG, across the year boundary. |
| December missing | Enterprise | Plain LAG; January must show NULL. |
| Two months missing in a row | Student (February, March) | Comparing April with January. |
| Complete across December to January | Basic | Month-number arithmetic that breaks in January. |
| Plan that starts later than the others | Family (October) | Its first row has no previous month. |