Last active
December 2, 2020 17:21
-
-
Save romain9292/15c00ce6d4b54e0ea3d98b72bc2ccd2f to your computer and use it in GitHub Desktop.
[Growth rate using SQL in BigQuery] 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