Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. -- use this for origin level tables
  2. -- origin level
  3. SELECT
  4. region,
  5. fy_week,
  6. visits AS visits,
  7. amount AS amount,
  8. hours AS hours,
  9. orders AS orders,
  10. products) AS products,
  11. orders / NULLIF(visits::NUMERIC, 0) AS "conversion",
  12. amount / orders AS dpt,
  13. products / orders AS upt,
  14. visits / NULLIF(hours::NUMERIC, 0) AS tphw,
  15. amount / NULLIF(hours::NUMERIC, 0) AS sph
  16. FROM analytics.finance_r0 -- CHOOSE CORRECT TABLE
  17. WHERE fy_week='2019-W24' -- FISCAL UNIT OF CHOICE
  18. GROUP BY
  19. region,
  20. fy_week
  21.  
  22. UNION
  23.  
  24. -- region level
  25. SELECT
  26. region,
  27. fy_week,
  28. SUM(visits) AS visits,
  29. SUM(amount) AS amount,
  30. SUM(hours) AS hours,
  31. SUM(orders) AS orders,
  32. SUM(products) AS products,
  33. SUM(orders) / NULLIF(SUM(visits::NUMERIC), 0) AS "conversion",
  34. SUM(amount) / SUM(orders) AS dpt,
  35. SUM(products) / SUM(orders) AS upt,
  36. SUM(visits) / NULLIF(SUM(hours::NUMERIC), 0) AS tphw,
  37. SUM(amount) / NULLIF(SUM(hours::NUMERIC), 0) AS sph
  38. FROM analytics.finance_r0
  39. WHERE fy_week='2019-W24' -- FISCAL UNIT OF CHOICE
  40. GROUP BY
  41. region,
  42. fy_week;
  43.  
  44. UNION
  45.  
  46. -- province level
  47. SELECT
  48. zone,
  49. fy_week,
  50. SUM(visits) AS visits,
  51. SUM(amount) AS amount,
  52. SUM(hours) AS hours,
  53. SUM(orders) AS orders,
  54. SUM(products) AS products,
  55. SUM(orders) / NULLIF(SUM(visits::NUMERIC), 0) AS "conversion",
  56. SUM(amount) / SUM(orders) AS dpt,
  57. SUM(products) / SUM(orders) AS upt,
  58. SUM(visits) / NULLIF(SUM(hours::NUMERIC), 0) AS tphw,
  59. SUM(amount) / NULLIF(SUM(hours::NUMERIC), 0) AS sph
  60. FROM analytics.finance_r0
  61. WHERE fy_week='2019-W24' -- FISCAL UNIT OF CHOICE
  62. GROUP BY
  63. zone,
  64. fy_week;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement