Month-over-month revenue growth
Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.
Write a query that returns the revenue of every month that has orders, and its growth compared with the previous month in the result.
Return the columns:
month: the first day of the month, as aDATE.revenue: the month's totalamount.growth_pct:(revenue - previous revenue) / previous revenue * 100, rounded to 1 decimal place.NULLfor the first month.
Order the rows by month. Months without orders don't appear, so "previous month" means the previous row.
Example
orders
| id | created_at | amount |
|---|---|---|
| 1 | 2024-01-05 10:00:00 | 400.00 |
| 2 | 2024-01-20 18:30:00 | 600.00 |
| 3 | 2024-02-11 09:15:00 | 1500.00 |
| 4 | 2024-03-03 12:00:00 | 1200.00 |
Result:
| month | revenue | growth_pct |
|---|---|---|
| 2024-01-01 | 1000.00 | NULL |
| 2024-02-01 | 1500.00 | 50.0 |
| 2024-03-01 | 1200.00 | -20.0 |
Tip
DATE_TRUNC buckets a timestamp into its month, and LAG reads a value from the previous row. In Postgres, ROUND(x, 1) needs x to be a NUMERIC.
Schema
CREATE TABLE orders (
id INT PRIMARY KEY,
created_at TIMESTAMP NOT NULL,
amount NUMERIC(10, 2) NOT NULL
);Approach
First aggregate to one row per month in a CTE, then compare each month with the one before it using LAG:
WITH monthly AS (
SELECT DATE_TRUNC('month', created_at)::date AS month, SUM(amount) AS revenue
FROM orders
GROUP BY 1
)
SELECT
month,
revenue,
ROUND(
(revenue - LAG(revenue) OVER (ORDER BY month)) * 100 / LAG(revenue) OVER (ORDER BY month),
1
) AS growth_pct
FROM monthly
ORDER BY month;DATE_TRUNC('month', ts)returns the timestamp at the start of its month, so every order in January lands on2024-01-01 00:00. The::datecast drops the time.LAG(revenue)isNULLon the first row, andNULLspreads through the arithmetic, so the first month getsNULLwithout a special case.GROUP BY 1groups by the first selected column. Handy, but some teams prefer naming the expression.
Pitfalls
- Integer division: if
revenuewere anINT,50 / 100would be0. Here it'sNUMERIC, and multiplying by100before dividing keeps the precision. - Rounding a float: Postgres has
ROUND(numeric, int)but notROUND(double precision, int). Cast with::numericif your expression became a float. - Division by zero: a month with zero revenue would fail the next row. Wrap the divisor in
NULLIF(LAG(...), 0). - Filtering by date with
WHERE DATE_TRUNC('month', created_at) = ...can't use an index oncreated_at. Prefer a range:created_at >= '2024-02-01' AND created_at < '2024-03-01'.
Follow-up: missing months
To show months with no orders as 0, generate the calendar with generate_series('2024-01-01'::date, '2024-12-01', interval '1 month') and LEFT JOIN the monthly totals to it.
Reference solution
WITH monthly AS (
SELECT DATE_TRUNC('month', created_at)::date AS month, SUM(amount) AS revenue
FROM orders
GROUP BY 1
)
SELECT
month,
revenue,
ROUND(
(revenue - LAG(revenue) OVER (ORDER BY month)) * 100 / LAG(revenue) OVER (ORDER BY month),
1
) AS growth_pct
FROM monthly
ORDER BY month;