Business Analytics · Problem 82 of 118

Monthly Revenue and Growth

HardBusiness AnalyticsShop data

Revenue is the sum of quantity * unit_price over the lines of orders whose status is not 'cancelled', counted in the month of order_date.

For each month of 2025 return month (like '2025-03'), revenue (rounded to 2 decimals) and growth_percent: the change against the previous month in percent, rounded to 1 decimal (NULL for the first month). Sort by month.

Hint 1

Build the monthly revenue in a CTE, then use LAG(revenue) for the previous month.

Hint 2

Growth is (this - previous) / previous * 100. Use 100.0 to avoid integer division.

Reference solution

WITH monthly AS (
  SELECT STRFTIME('%Y-%m', o.order_date) AS month,
         ROUND(SUM(oi.quantity * oi.unit_price), 2) AS revenue
  FROM orders AS o
  JOIN order_items AS oi ON oi.order_id = o.order_id
  WHERE o.status <> 'cancelled'
    AND o.order_date BETWEEN '2025-01-01' AND '2025-12-31'
  GROUP BY STRFTIME('%Y-%m', o.order_date)
)
SELECT month,
       revenue,
       ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY month)) / LAG(revenue) OVER (ORDER BY month), 1) AS growth_percent
FROM monthly
ORDER BY month;

Three skills in one query: a join (orders to lines), a group (by month), and a window function (compare with the row before). Define "revenue" first and exactly: cancelled orders never produced money, so they must be excluded. In real analytics most arguments are about definitions, not SQL. The growth formula divides by the previous value; dividing by the current one gives a different, wrong percentage.