Created
May 16, 2019 20:44
-
-
Save ToddKerpelman/7a29aecd6cafeefd49070f5d05716d2e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 analytics_data AS ( | |
SELECT user_pseudo_id, event_timestamp, event_name, | |
app_info.version AS app_version, -- This is new! | |
UNIX_MICROS(TIMESTAMP("2018-08-01 00:00:00", "-7:00")) AS start_day, | |
3600*1000*1000*24*7 AS one_week_micros | |
FROM `firebase-public-project.analytics_153293282.events_*` | |
WHERE _table_suffix BETWEEN '20180731' AND '20180829' | |
) | |
SELECT week_0_cohort / week_0_cohort AS week_0_pct, | |
week_1_cohort / week_0_cohort AS week_1_pct, | |
week_2_cohort / week_0_cohort AS week_2_pct, | |
week_3_cohort / week_0_cohort AS week_3_pct | |
FROM ( | |
WITH week_3_users AS ( | |
SELECT DISTINCT user_pseudo_id | |
FROM analytics_data | |
WHERE event_timestamp BETWEEN start_day+(3*one_week_micros) AND start_day+(4*one_week_micros) | |
), | |
week_2_users AS ( | |
SELECT DISTINCT user_pseudo_id | |
FROM analytics_data | |
WHERE event_timestamp BETWEEN start_day+(2*one_week_micros) AND start_day+(3*one_week_micros) | |
), | |
week_1_users AS ( | |
SELECT DISTINCT user_pseudo_id | |
FROM analytics_data | |
WHERE event_timestamp BETWEEN start_day+(1*one_week_micros) AND start_day+(2*one_week_micros) | |
), | |
week_0_users AS ( | |
SELECT DISTINCT user_pseudo_id | |
FROM analytics_data | |
WHERE event_name = 'first_open' | |
AND app_version = "2.62" -- This bit is new, too! | |
AND event_timestamp BETWEEN start_day AND start_day+(1*one_week_micros) | |
) | |
SELECT | |
(SELECT count(*) | |
FROM week_0_users) AS week_0_cohort, | |
(SELECT count(*) | |
FROM week_1_users | |
JOIN week_0_users USING (user_pseudo_id)) AS week_1_cohort, | |
(SELECT count(*) | |
FROM week_2_users | |
JOIN week_0_users USING (user_pseudo_id)) AS week_2_cohort, | |
(SELECT count(*) | |
FROM week_3_users | |
JOIN week_0_users USING (user_pseudo_id)) AS week_3_cohort | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment