im trying to do cohort analysis using SQL server i was looking at this online
`-- (user_id, cohort_month), each
with cohort_items as (
select
date_trunc('month', U.timestamp)::date as cohort_month,
id as user_id
from users U
order by 1, 2
)
After that, we build user_activities which
-- (user_id, month_number): user X has activity in month number X
WITH user_activities as (
select
A.user_id,
MONTH_DIFF(
date_trunc('month', A.timestamp)::date,
C.cohort_month
) as month_number
from public.activities A
left join cohort_items C ON A.user_id = C.user_id
group by 1, 2
)
how to use cohort_items is the second cte ?
CodePudding user response:
You can use something like this.
WITH X (SELET * FROM TABLE),
Y AS (SELECT * FROM X);
CodePudding user response:
with cohort_items as (
select
date_trunc('month', U.timestamp)::date as cohort_month,
id as user_id
from users U
)
,user_activities as (
select
A.user_id,
MONTH_DIFF(
date_trunc('month', A.timestamp)::date,
C.cohort_month
) as month_number
from public.activities A
left join cohort_items C ON A.user_id = C.user_id
group by 1, 2
)
