Skip to content

Instantly share code, notes, and snippets.

@gxercavins
Created January 12, 2020 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gxercavins/d49e704134b985910c40d4e9c5fb3ab4 to your computer and use it in GitHub Desktop.
Save gxercavins/d49e704134b985910c40d4e9c5fb3ab4 to your computer and use it in GitHub Desktop.
SO question 59702176
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