Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 5
- SELECT DISTINCT DATE_TRUNC('month', created_at)::date AS reg_month,
- COUNT(user_id) OVER (ORDER BY DATE_TRUNC('month', created_at)::date) reg_users_cnt
- FROM tools_shop.users
- 6
- WITH cte AS
- (SELECT CAST(DATE_TRUNC('month', created_at) AS DATE) AS MONTH,
- SUM(costs) OVER (
- ORDER BY CAST(DATE_TRUNC('month', created_at) AS DATE)) AS cum_cost
- FROM tools_shop.costs
- WHERE CAST(DATE_TRUNC('month', created_at) AS DATE) BETWEEN '2017-01-01' AND '2018-12-31' )
- SELECT DISTINCT MONTH,
- cum_cost
- FROM cte;
- 7
- WITH
- pz AS ( SELECT CAST(DATE_TRUNC('month',e.event_time) AS date) AS event_month,
- COUNT(DISTINCT e.event_id) event_cnt
- FROM tools_shop.events AS e
- JOIN tools_shop.orders AS o ON e.user_id = o.user_id
- WHERE e.event_name = 'view_item'
- GROUP BY event_month )
- SELECT *,
- SUM (event_cnt) OVER (
- ORDER BY event_month) AS cumsum
- FROM pz
- 8
- SELECT order_id,
- CAST(DATE_TRUNC('month', created_at) AS DATE) order_month,
- SUM(total_amt) AS order_revenue,
- COUNT(order_id) OVER (my_window),
- SUM(total_amt) OVER (my_window)
- FROM tools_shop.orders
- GROUP BY order_month,order_id,total_amt
- WINDOW my_window AS ( ORDER BY CAST(DATE_TRUNC('month', created_at) AS DATE) )
- 9
- WITH
- pz AS ( SELECT CAST(DATE_TRUNC('month', created_at) AS DATE) AS acq_month,
- SUM(costs) AS month_costs
- FROM tools_shop.costs
- GROUP BY acq_month )
- SELECT *,
- month_costs - LAG(month_costs,1, month_costs) OVER ( ORDER BY acq_month) AS delta_costs
- FROM pz
- 10
- WITH pz AS ( SELECT CAST( DATE_TRUNC( 'year', created_at) AS DATE) AS year_name,
- SUM(total_amt) AS sum_total
- FROM tools_shop.orders
- GROUP BY year_name )
- SELECT *,
- LEAD(sum_total,1,sum_total) OVER (ORDER BY year_name) - sum_total
- FROM pz
Advertisement
Add Comment
Please, Sign In to add comment