Created
January 12, 2020 12:02
-
-
Save gxercavins/d49e704134b985910c40d4e9c5fb3ab4 to your computer and use it in GitHub Desktop.
SO question 59702176
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 | |
authors AS ( | |
SELECT | |
author, | |
DATE_TRUNC(DATE(time_ts), MONTH) AS month | |
FROM | |
`bigquery-public-data.hacker_news.stories` | |
WHERE | |
author IS NOT NULL | |
GROUP BY 1,2) | |
SELECT | |
*, | |
ROUND(100*SAFE_DIVIDE(num_returning_users, | |
num_users),2) AS retention | |
FROM ( | |
SELECT | |
a.month, | |
COUNT(a.author) AS num_users, | |
COUNTIF(EXISTS | |
( | |
SELECT | |
1 | |
FROM | |
authors as b | |
WHERE | |
a.author = b.author | |
AND | |
a.month = DATE_ADD(b.month, INTERVAL 1 MONTH) | |
)) AS num_returning_users | |
FROM | |
authors as a | |
GROUP BY 1 | |
ORDER BY 1 | |
LIMIT 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment