Skip to content

Instantly share code, notes, and snippets.

@romain9292
Created December 16, 2020 10:43
Show Gist options
  • Save romain9292/b2cdba6dd0ef38b1ac6e931b380c2b44 to your computer and use it in GitHub Desktop.
Save romain9292/b2cdba6dd0ef38b1ac6e931b380c2b44 to your computer and use it in GitHub Desktop.
[Growth rate using SQL in BigQuery - Part 3] Computing a month over month revenue growth rate #SQL #BigQuery
WITH
revenue_over_month AS (
SELECT
DATE_TRUNC(PARSE_DATE('%Y%m%d',
date),MONTH) AS months,
ROUND(SUM(totals.totalTransactionRevenue)/10e+6,2) AS revenue,
FROM
`bigquery-public-data.google_analytics_sample.ga_sessions_20*`
GROUP BY
1)
-- Main Query
SELECT
*,
ROUND((revenue - LAG(revenue) OVER(ORDER BY months ASC)) / LAG(revenue) OVER(ORDER BY months ASC) * 100,2) AS growth_rate
FROM
revenue_over_month
ORDER BY
months ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment