Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH user_count AS (
- SELECT
- orders.user_id,
- MIN (DATE_TRUNC('month', events.event_time)::date) AS start
- FROM tools_shop.orders AS orders
- INNER JOIN tools_shop.users AS users ON orders.user_id = users.user_id
- INNER JOIN tools_shop.events AS events ON events.user_id = users.user_id
- GROUP BY orders.user_id
- )
- SELECT
- user_count.start AS start,
- CAST( DATE_TRUNC('month', events.event_time) AS date) as month,
- COUNT(DISTINCT user_count.user_id)
- FROM user_count INNER JOIN tools_shop.events AS events ON events.user_id = user_count.user_id
- GROUP BY start, month
Advertisement
Add Comment
Please, Sign In to add comment