Month-on-month growth
With last month's figure on the same row, growth is ordinary arithmetic:
SELECT month, revenue,
revenue - LAG(revenue) OVER (ORDER BY month) AS change,
100.0 * (revenue - LAG(revenue) OVER (ORDER BY month))
/ LAG(revenue) OVER (ORDER BY month) AS growth_pct
FROM monthlygrowth_pct of 12.5 means revenue grew 12.5% on the month before; −5 means it fell 5%.
Writing LAG(revenue) OVER (ORDER BY month) three times is clumsy. Put it in a CTE once, as
prev_revenue, and then the growth sum reads like a spreadsheet formula:
100.0 * (revenue - prev_revenue) / prev_revenueUse 100.0 rather than 100. In some databases (SQL Server, PostgreSQL) dividing two whole numbers
throws away the decimals.
Careful with gaps:
LAGtakes the previous row, not the previous month. If a month has no sales at all it has no row, andLAGreaches back to the month before that. Brightwell sells something every month, so it's safe here, but check before relying on it.