Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
574 B
12
Indexable
Never
WITH first_ts AS
      (SELECT u.user_id,
             DATE_TRUNC('month', MIN(event_time))::date AS first_date
      FROM tools_shop.users u
      JOIN tools_shop.orders o ON u.user_id = o.user_id
      JOIN tools_shop.events e ON u.user_id = e.user_id
      GROUP BY 1),
      
 all_ts AS     
(SELECT  e.user_id,
       DATE_TRUNC('month', e.event_time)::date AS dt_event     
FROM first f
JOIN tools_shop.events e ON e.user_id= f.user_id
group by 1,2)

SELECT DISTINCT first_date,
count(user_id) OVER (PARTITION BY first_date) AS cohort_cnt
FROM all_ts;