Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. WITH cte_mo_filter AS (
  2. SELECT
  3. gt_child.id,
  4. gt_child.name
  5. FROM geo_tags gt_parent
  6. JOIN geo_tags gt_child ON gt_child.lft BETWEEN gt_parent.lft AND gt_parent.rgt
  7. WHERE
  8. gt_parent.id = 2208163
  9. AND gt_child.level = 3
  10. AND gt_child.is_archive = 0
  11. ), cte_has_metrics_check AS (
  12. SELECT
  13. mo.id,
  14. bool_and(m.geo_tag_id IS NULL) AS "hasNotMetrics"
  15. FROM cte_mo_filter mo
  16. LEFT JOIN new_reloc_prog_mo_metrics m ON m.geo_tag_id = mo.id
  17. GROUP BY mo.id
  18.  
  19. ), cte_2019_data AS (
  20. SELECT
  21. mo.id,
  22. coalesce(relocation_ppl, 0) AS relocation_ppl
  23. FROM cte_mo_filter mo
  24. LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2019
  25. ), cte_2020_data AS (
  26. SELECT
  27. mo.id,
  28. coalesce(relocation_ppl, 0) AS relocation_ppl
  29. FROM cte_mo_filter mo
  30. LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2020
  31. ), cte_2021_data AS (
  32. SELECT
  33. mo.id,
  34. coalesce(relocation_ppl, 0) AS relocation_ppl
  35. FROM cte_mo_filter mo
  36. LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2021
  37. ), cte_2022_data AS (
  38. SELECT
  39. mo.id,
  40. coalesce(relocation_ppl, 0) AS relocation_ppl
  41. FROM cte_mo_filter mo
  42. LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2022
  43. ), cte_2023_data AS (
  44. SELECT
  45. mo.id,
  46. coalesce(relocation_ppl, 0) AS relocation_ppl
  47. FROM cte_mo_filter mo
  48. LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2023
  49. ), cte_2024_data AS (
  50. SELECT
  51. mo.id,
  52. coalesce(relocation_ppl, 0) AS relocation_ppl
  53. FROM cte_mo_filter mo
  54. LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2024
  55. ), cte_2025_data AS (
  56. SELECT
  57. mo.id,
  58. coalesce(relocation_ppl, 0) AS relocation_ppl
  59. FROM cte_mo_filter mo
  60. LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2025
  61. )
  62. SELECT
  63. mo.name,
  64. d_2019.relocation_ppl AS relocation_ppl_2019,
  65. d_2020.relocation_ppl AS relocation_ppl_2020,
  66. d_2021.relocation_ppl AS relocation_ppl_2021,
  67. d_2022.relocation_ppl AS relocation_ppl_2022,
  68. d_2023.relocation_ppl AS relocation_ppl_2023,
  69. d_2024.relocation_ppl AS relocation_ppl_2024,
  70. d_2025.relocation_ppl AS relocation_ppl_2025
  71. FROM cte_mo_filter mo
  72. JOIN cte_2019_data d_2019 ON d_2019.id = mo.id
  73. JOIN cte_2020_data d_2020 ON d_2020.id = mo.id
  74. JOIN cte_2021_data d_2021 ON d_2021.id = mo.id
  75. JOIN cte_2022_data d_2022 ON d_2022.id = mo.id
  76. JOIN cte_2023_data d_2023 ON d_2023.id = mo.id
  77. JOIN cte_2024_data d_2024 ON d_2024.id = mo.id
  78. JOIN cte_2025_data d_2025 ON d_2025.id = mo.id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement