- with
- profiles as (select user_id, date_trunc('month', created_at)::date as chogort_st,
- count(*) over (partition by date_trunc('month', created_at))
- from tools_shop.users)
- select EXTRACT(month FROM AGE(DATE_TRUNC('month', o.paid_at), p.chogort_st)) AS lifetime,
- DATE_TRUNC('month', o.paid_at)::date AS order_date,
- p.chogort_st,
- p.count, o.total_amt
- from profiles p
- join tools_shop.orders o on o.user_id=p.user_id