Skip to main content
08

Month-over-month revenue growth

MediumCTELAGDATE_TRUNC

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 a DATE.
  • revenue: the month's total amount.
  • growth_pct: (revenue - previous revenue) / previous revenue * 100, rounded to 1 decimal place. NULL for the first month.

Order the rows by month. Months without orders don't appear, so "previous month" means the previous row.

Example

orders

idcreated_atamount
12024-01-05 10:00:00400.00
22024-01-20 18:30:00600.00
32024-02-11 09:15:001500.00
42024-03-03 12:00:001200.00

Result:

monthrevenuegrowth_pct
2024-01-011000.00NULL
2024-02-011500.0050.0
2024-03-011200.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

sql
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:

sql
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 on 2024-01-01 00:00. The ::date cast drops the time.
  • LAG(revenue) is NULL on the first row, and NULL spreads through the arithmetic, so the first month gets NULL without a special case.
  • GROUP BY 1 groups by the first selected column. Handy, but some teams prefer naming the expression.

Pitfalls

  • Integer division: if revenue were an INT, 50 / 100 would be 0. Here it's NUMERIC, and multiplying by 100 before dividing keeps the precision.
  • Rounding a float: Postgres has ROUND(numeric, int) but not ROUND(double precision, int). Cast with ::numeric if 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 on created_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

sql
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;