Untitled

From Bulky Leech, 2 Months ago, written in SQL, viewed 52 times.
URL http://codebin.org/view/6a4bf3b8 Embed
Download Paste or View Raw
  1. WITH profile AS
  2.   (SELECT u.user_id,
  3.           DATE_TRUNC('month', MIN(event_time))::DATE AS dt
  4.    FROM tools_shop.users u
  5.    JOIN tools_shop.orders o ON u.user_id = o.user_id
  6.    JOIN tools_shop.events e ON u.user_id = e.user_id
  7.    GROUP BY 1),
  8. sessions AS
  9.   (SELECT p.user_id,
  10.           DATE_TRUNC('month', event_time)::DATE AS session_dt
  11.    FROM tools_shop.events e
  12.    JOIN profile p ON p.user_id = e.user_id
  13.    GROUP BY 1,
  14.             2),
  15. cohort_users_cnt AS
  16.   (SELECT dt,
  17.           COUNT(user_id) AS cohort_users_cnt
  18.    FROM profile
  19.    GROUP BY 1)
  20.    
  21. SELECT p.dt AS cohort_dt,
  22.        session_dt,
  23.        COUNT(p.user_id) AS users_cnt,
  24.        cohort_users_cnt,
  25.        ROUND(COUNT(p.user_id) * 100.0 / cohort_users_cnt, 2) AS retention_rate
  26. FROM profile p
  27. JOIN sessions s ON p.user_id = s.user_id
  28. JOIN cohort_users_cnt cuc ON cuc.dt=p.dt
  29. GROUP BY 1,
  30.          2,
  31.          4
  32. ORDER BY 1, 2
  33.  
  34.  

Reply to "Untitled"

Here you can reply to the paste above