Month Over Month Revenue Growth
Return one row per calendar month in which at least one order was placed, with:
month— the first day of the month, as adaterevenue— the totalamountof that month's ordersprev_revenue— therevenueof the immediately preceding month that appears in this result, orNULLfor the first monthchange—revenue - prev_revenue, orNULLfor the first month
Order by month ascending.
Expected output
month | revenue | prev_revenue | change
-----------+---------+--------------+--------
2024-01-01 | 800.00 | NULL | NULL
2024-02-01 | 400.00 | 800.00 | -400.00
2024-04-01 | 1200.00 | 400.00 | 800.00
2024-05-01 | 1000.00 | 1200.00 | -200.00
Editorial
Two steps that have to stay separate: aggregate to one row per month, then look backwards within that aggregated result.
WITH monthly AS (
SELECT date_trunc('month', placed_on)::date AS month,
sum(amount) AS revenue
FROM "order"
GROUP BY 1
)
SELECT month,
revenue,
lag(revenue) OVER (ORDER BY month) AS prev_revenue,
revenue - lag(revenue) OVER (ORDER BY month) AS change
FROM monthly
ORDER BY month;
Why the CTE is not optional
A window function runs after GROUP BY, so you can
write lag(sum(amount)) OVER (ORDER BY date_trunc('month', placed_on))
in one pass and it works. What you cannot do is put a window function in the
GROUP BY or filter on one in WHERE — both run earlier.
The CTE costs nothing and makes the order of operations visible, which matters
more the moment someone asks you to filter on change.
The first row is NULL, and that is the answer
lag() has nothing to look back at for January, so it returns
NULL, and revenue - NULL is NULL. The statement asks for NULL, so
leave it. If a report wanted a number you would write
lag(revenue, 1, 0) — the third argument is the default — but that
is a different claim: it says the previous month's revenue was zero rather than
unknown.
The missing month
March has no orders, so it has no row, so April's lag is
February. The statement says "the immediately preceding month that appears in
this result", which is exactly what lag gives. Read it the other
way — "the previous calendar month" — and April's answer should be NULL, and
lag is the wrong tool. You would need a generated calendar to join
against:
SELECT g.month, m.revenue
FROM generate_series(DATE '2024-01-01', DATE '2024-05-01', INTERVAL '1 month') AS g(month)
LEFT JOIN monthly m ON m.month = g.month::date;
Volunteering that distinction is the point of the problem. "Previous row" and "previous month" are the same thing only in data with no gaps, and real data has gaps.
Percentage instead of difference
round(100.0 * (revenue - lag(revenue) OVER (ORDER BY month))
/ lag(revenue) OVER (ORDER BY month), 1) AS pct
Fine, with one caveat worth saying out loud: it divides by the previous
month, so a month following a zero gives a division-by-zero error rather than
"infinite growth". Guard it with nullif(lag(revenue) OVER (...), 0)
and you get NULL instead, which is the honest answer.
What an interviewer is checking
That you reach for lag rather than self-joining the aggregate to
itself on month = month - interval '1 month'. The self-join works
and is four lines longer, and it answers the calendar-month reading instead —
which is a fine answer as long as you know which question you just answered.
order
8 rows
| idinteger | placed_ondate | amountnumeric(10,2) |
|---|---|---|
| 1 | 1704326400000000 | 500 |
| 2 | 1705622400000000 | 300 |
| 3 | 1706832000000000 | 250 |
| 4 | 1708992000000000 | 150 |
| 5 | 1711929600000000 | 1000 |
Showing 5 of 8 rows.