Dina_J

C_R

Nov 29th, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH user_count AS (
  2. SELECT
  3.     orders.user_id,
  4.     MIN (DATE_TRUNC('month', events.event_time)::date) AS start
  5. FROM tools_shop.orders AS orders
  6. INNER JOIN tools_shop.users AS users ON orders.user_id = users.user_id
  7. INNER JOIN tools_shop.events AS events ON events.user_id = users.user_id
  8. GROUP BY orders.user_id
  9.  
  10. )
  11.  
  12. SELECT
  13.     user_count.start AS start,
  14.     CAST( DATE_TRUNC('month', events.event_time) AS date) as month,
  15.     COUNT(DISTINCT user_count.user_id)
  16.    
  17. FROM user_count INNER JOIN tools_shop.events AS events ON events.user_id = user_count.user_id
  18. GROUP BY start, month
Advertisement
Add Comment
Please, Sign In to add comment