Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
580 B
2
Indexable
Never
WITH profiles as (SELECT
    user_id ,
    DATE_TRUNC('month', created_at)::date AS start_cohort,
    COUNT(*) OVER (PARTITION BY DATE_TRUNC('month', created_at)::date) as cohort_size
FROM
    tools_shop.users
GROUP BY
    user_id, DATE_TRUNC('month', created_at))
    
SELECT EXTRACT(month FROM AGE(DATE_TRUNC('month', o.paid_at)::date, profiles.start_cohort)) AS lifetime,
       o.paid_at::date AS order_date,
       start_cohort,
       cohort_size,
       total_amt
       FROM profiles
       JOIN tools_shop.orders o ON profiles.user_id = o.user_id;