Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH cte_mo_filter AS (
- SELECT
- gt_child.id,
- gt_child.name
- FROM geo_tags gt_parent
- JOIN geo_tags gt_child ON gt_child.lft BETWEEN gt_parent.lft AND gt_parent.rgt
- WHERE
- gt_parent.id = 2208163
- AND gt_child.level = 3
- AND gt_child.is_archive = 0
- ), cte_has_metrics_check AS (
- SELECT
- mo.id,
- bool_and(m.geo_tag_id IS NULL) AS "hasNotMetrics"
- FROM cte_mo_filter mo
- LEFT JOIN new_reloc_prog_mo_metrics m ON m.geo_tag_id = mo.id
- GROUP BY mo.id
- ), cte_2019_data AS (
- SELECT
- mo.id,
- coalesce(relocation_ppl, 0) AS relocation_ppl
- FROM cte_mo_filter mo
- LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2019
- ), cte_2020_data AS (
- SELECT
- mo.id,
- coalesce(relocation_ppl, 0) AS relocation_ppl
- FROM cte_mo_filter mo
- LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2020
- ), cte_2021_data AS (
- SELECT
- mo.id,
- coalesce(relocation_ppl, 0) AS relocation_ppl
- FROM cte_mo_filter mo
- LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2021
- ), cte_2022_data AS (
- SELECT
- mo.id,
- coalesce(relocation_ppl, 0) AS relocation_ppl
- FROM cte_mo_filter mo
- LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2022
- ), cte_2023_data AS (
- SELECT
- mo.id,
- coalesce(relocation_ppl, 0) AS relocation_ppl
- FROM cte_mo_filter mo
- LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2023
- ), cte_2024_data AS (
- SELECT
- mo.id,
- coalesce(relocation_ppl, 0) AS relocation_ppl
- FROM cte_mo_filter mo
- LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2024
- ), cte_2025_data AS (
- SELECT
- mo.id,
- coalesce(relocation_ppl, 0) AS relocation_ppl
- FROM cte_mo_filter mo
- LEFT JOIN new_reloc_prog_mo_metrics m ON mo.id = m.geo_tag_id AND m.period = 2025
- )
- SELECT
- mo.name,
- d_2019.relocation_ppl AS relocation_ppl_2019,
- d_2020.relocation_ppl AS relocation_ppl_2020,
- d_2021.relocation_ppl AS relocation_ppl_2021,
- d_2022.relocation_ppl AS relocation_ppl_2022,
- d_2023.relocation_ppl AS relocation_ppl_2023,
- d_2024.relocation_ppl AS relocation_ppl_2024,
- d_2025.relocation_ppl AS relocation_ppl_2025
- FROM cte_mo_filter mo
- JOIN cte_2019_data d_2019 ON d_2019.id = mo.id
- JOIN cte_2020_data d_2020 ON d_2020.id = mo.id
- JOIN cte_2021_data d_2021 ON d_2021.id = mo.id
- JOIN cte_2022_data d_2022 ON d_2022.id = mo.id
- JOIN cte_2023_data d_2023 ON d_2023.id = mo.id
- JOIN cte_2024_data d_2024 ON d_2024.id = mo.id
- JOIN cte_2025_data d_2025 ON d_2025.id = mo.id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement