-
-
Save danielgranat/92ceca67b43994f7c5f3fce7bdb312a7 to your computer and use it in GitHub Desktop.
Cohort Query Example
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 | |
time_frame as ( | |
select current_date - 14 | |
), | |
population as ( | |
select created_at::date as cohort_date, id as unique_id | |
from organizations | |
where created_at > (select * from time_frame) | |
), | |
activity as ( | |
select created_at::date as activity_date, org_id as unique_id, cohort_date | |
from events | |
join population on population.unique_id = org_id | |
where created_at > (select * from time_frame) | |
), | |
population_agg as ( | |
select cohort_date, count(distinct unique_id) as total | |
from population | |
group by 1 | |
) | |
select activity.cohort_date as date, | |
date_part('day',activity_date - activity.cohort_date) as day, | |
count(distinct unique_id) as value, | |
total | |
from activity | |
join population_agg on activity.cohort_date = population_agg.cohort_date | |
group by 1 , 2, 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment