Dina_J

ltv3

Nov 30th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   WITH user_profile AS
  2.     (SELECT u.user_id,
  3.            EXTRACT(MONTH FROM AGE(DATE_TRUNC('month', o.created_at), DATE_TRUNC('month', u.created_at)::date)) AS lifetime,
  4.            DATE_TRUNC('month', o.created_at)::date AS order_month,
  5.            DATE_TRUNC('month', u.created_at)::date AS start_cohort,
  6.            COUNT(*) OVER (PARTITION BY DATE_TRUNC('month', u.created_at)) AS cohort_size,
  7.            total_amt AS revenue
  8.            FROM tools_shop.users AS u
  9.            JOIN tools_shop.orders AS o ON u.user_id = o.user_id)
  10.  
  11. SELECT
  12.     lifetime,
  13.     start_cohort,
  14.     cohort_size,
  15.     SUM(revenue) OVER (PARTITION BY start_cohort ORDER BY lifetime) / cohort_size AS ltv
  16.  
  17. FROM user_profile AS up
  18.  
Advertisement
Add Comment
Please, Sign In to add comment