andrey_vetrov

Untitled

May 5th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. 5
  2. SELECT DISTINCT DATE_TRUNC('month', created_at)::date AS reg_month,
  3. COUNT(user_id) OVER (ORDER BY DATE_TRUNC('month', created_at)::date) reg_users_cnt
  4. FROM tools_shop.users
  5.  
  6. 6
  7. WITH cte AS
  8. (SELECT CAST(DATE_TRUNC('month', created_at) AS DATE) AS MONTH,
  9. SUM(costs) OVER (
  10. ORDER BY CAST(DATE_TRUNC('month', created_at) AS DATE)) AS cum_cost
  11. FROM tools_shop.costs
  12. WHERE CAST(DATE_TRUNC('month', created_at) AS DATE) BETWEEN '2017-01-01' AND '2018-12-31' )
  13. SELECT DISTINCT MONTH,
  14. cum_cost
  15. FROM cte;
  16.  
  17. 7
  18. WITH
  19. pz AS ( SELECT CAST(DATE_TRUNC('month',e.event_time) AS date) AS event_month,
  20. COUNT(DISTINCT e.event_id) event_cnt
  21. FROM tools_shop.events AS e
  22. JOIN tools_shop.orders AS o ON e.user_id = o.user_id
  23. WHERE e.event_name = 'view_item'
  24. GROUP BY event_month )
  25.  
  26. SELECT *,
  27. SUM (event_cnt) OVER (
  28. ORDER BY event_month) AS cumsum
  29. FROM pz
  30.  
  31. 8
  32. SELECT order_id,
  33. CAST(DATE_TRUNC('month', created_at) AS DATE) order_month,
  34. SUM(total_amt) AS order_revenue,
  35. COUNT(order_id) OVER (my_window),
  36. SUM(total_amt) OVER (my_window)
  37. FROM tools_shop.orders
  38.  
  39. GROUP BY order_month,order_id,total_amt
  40. WINDOW my_window AS ( ORDER BY CAST(DATE_TRUNC('month', created_at) AS DATE) )
  41.  
  42. 9
  43. WITH
  44. pz AS ( SELECT CAST(DATE_TRUNC('month', created_at) AS DATE) AS acq_month,
  45. SUM(costs) AS month_costs
  46. FROM tools_shop.costs
  47. GROUP BY acq_month )
  48.  
  49. SELECT *,
  50. month_costs - LAG(month_costs,1, month_costs) OVER ( ORDER BY acq_month) AS delta_costs
  51. FROM pz
  52.  
  53. 10
  54. WITH pz AS ( SELECT CAST( DATE_TRUNC( 'year', created_at) AS DATE) AS year_name,
  55. SUM(total_amt) AS sum_total
  56. FROM tools_shop.orders
  57. GROUP BY year_name )
  58.  
  59. SELECT *,
  60. LEAD(sum_total,1,sum_total) OVER (ORDER BY year_name) - sum_total
  61. FROM pz
Advertisement
Add Comment
Please, Sign In to add comment