Advertisement
AnnaCh1971

Продвинутый SQL когортный анализ LTV задача 2

May 30th, 2023 (edited)
1,942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH user_profile AS (SELECT DISTINCT user_id,
  2.                              DATE_TRUNC ('month', created_at )::date AS start_cohort,
  3.                              COUNT (*) OVER (PARTITION BY DATE_TRUNC ('month', created_at )::date) AS cohort_size
  4.                              FROM tools_shop.users)
  5.  
  6.  
  7. SELECT EXTRACT(MONTH FROM AGE(DATE_TRUNC('day', o.created_at), up.start_cohort)) AS lifetime,
  8.         DATE_TRUNC ('month', o.created_at )::date AS order_date,
  9.         start_cohort,
  10.         cohort_size,
  11.         total_amt
  12. FROM user_profile up
  13. JOIN tools_shop.orders o ON up.user_id = o.user_id
  14.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement