Created
December 16, 2020 10:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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