diff --git a/airflow/include/sql/sparte/macros/insee/merge_flux_population_by_admin_level.sql b/airflow/include/sql/sparte/macros/insee/merge_flux_population_by_admin_level.sql new file mode 100644 index 000000000..e56401cda --- /dev/null +++ b/airflow/include/sql/sparte/macros/insee/merge_flux_population_by_admin_level.sql @@ -0,0 +1,37 @@ +{% macro merge_flux_population_by_admin_level( + group_by_column, + code_name +) %} +SELECT + {{ group_by_column }} as {{ code_name }}, + {% set last_available_year = 2020 %} + {% set first_available_year = 2009 %} + {% set ns = namespace(continued=false) %} + {% for start_year in range(2009, last_available_year + 1) %} + {% for end_year in range(2009, last_available_year + 1) -%} + {% if start_year > end_year -%} + {% set ns.continued = true %} + {% continue %} + {% else %} + {% set ns.continued = false %} + {% endif %} + sum(population_{{ start_year }}_{{ end_year + 1 }}) + as population_{{ start_year }}_{{ end_year + 1 }} + {% if not loop.last and not ns.continued -%}, {% endif %} + {% endfor %} {% if not loop.last and not ns.continued -%}, {% endif %} + {% endfor %} +FROM + {{ ref('flux_population') }} as flux_population +LEFT JOIN + {{ ref('commune') }} + ON commune.code = flux_population.code_commune +LEFT JOIN + {{ ref('scot_communes') }} as scot_communes + ON commune.code = scot_communes.commune_code +-- la condition suivante est nécessaire car un grand nombre de communes ne fait pas partie d'un SCOT, +-- et plus rarement certaines communes ne font pas partie d'un EPCI +WHERE + {{ group_by_column }} IS NOT NULL +GROUP BY + {{ group_by_column }} +{% endmacro %} diff --git a/airflow/include/sql/sparte/models/insee/aggregated/flux_population_departements.sql b/airflow/include/sql/sparte/models/insee/aggregated/flux_population_departements.sql new file mode 100644 index 000000000..7d482ae92 --- /dev/null +++ b/airflow/include/sql/sparte/models/insee/aggregated/flux_population_departements.sql @@ -0,0 +1,8 @@ +{{ config(materialized='table') }} + +{{ + merge_flux_population_by_admin_level( + 'departement', + 'code_departement' + ) +}} diff --git a/airflow/include/sql/sparte/models/insee/aggregated/flux_population_epcis.sql b/airflow/include/sql/sparte/models/insee/aggregated/flux_population_epcis.sql new file mode 100644 index 000000000..3532fbc49 --- /dev/null +++ b/airflow/include/sql/sparte/models/insee/aggregated/flux_population_epcis.sql @@ -0,0 +1,8 @@ +{{ config(materialized='table') }} + +{{ + merge_flux_population_by_admin_level( + 'epci', + 'code_epci' + ) +}} diff --git a/airflow/include/sql/sparte/models/insee/aggregated/flux_population_regions.sql b/airflow/include/sql/sparte/models/insee/aggregated/flux_population_regions.sql new file mode 100644 index 000000000..ccb90408b --- /dev/null +++ b/airflow/include/sql/sparte/models/insee/aggregated/flux_population_regions.sql @@ -0,0 +1,8 @@ +{{ config(materialized='table') }} + +{{ + merge_flux_population_by_admin_level( + 'region', + 'code_region' + ) +}} diff --git a/airflow/include/sql/sparte/models/insee/aggregated/flux_population_scots.sql b/airflow/include/sql/sparte/models/insee/aggregated/flux_population_scots.sql new file mode 100644 index 000000000..799f5d16e --- /dev/null +++ b/airflow/include/sql/sparte/models/insee/aggregated/flux_population_scots.sql @@ -0,0 +1,8 @@ +{{ config(materialized='table') }} + +{{ + merge_flux_population_by_admin_level( + 'id_scot', + 'code_scot' + ) +}} diff --git a/airflow/include/sql/sparte/models/insee/flux_population.sql b/airflow/include/sql/sparte/models/insee/flux_population.sql new file mode 100644 index 000000000..28a7f4eec --- /dev/null +++ b/airflow/include/sql/sparte/models/insee/flux_population.sql @@ -0,0 +1,49 @@ +{{ + config( + materialized='table', + indexes=[{'columns': ['code_commune'], 'type': 'btree'}] + ) +}} +with flux as ( + SELECT + code_commune, + (population_2010 - population_2009) as population_2009_2010, + (population_2011 - population_2010) as population_2010_2011, + (population_2012 - population_2011) as population_2011_2012, + (population_2013 - population_2012) as population_2012_2013, + (population_2014 - population_2013) as population_2013_2014, + (population_2015 - population_2014) as population_2014_2015, + (population_2016 - population_2015) as population_2015_2016, + (population_2017 - population_2016) as population_2016_2017, + (population_2018 - population_2017) as population_2017_2018, + (population_2019 - population_2018) as population_2018_2019, + (population_2020 - population_2019) as population_2019_2020, + (population_2021 - population_2020) as population_2020_2021 + FROM + {{ ref('population_cog_2024') }} +) +SELECT + code_commune, + {% set last_available_year = 2020 %} + {% set first_available_year = 2009 %} + {% set ns = namespace(continued=false) %} + {% for start_year in range(2009, last_available_year + 1) %} + {% for end_year in range(2009, last_available_year + 1) -%} + {% if start_year > end_year -%} + {% set ns.continued = true %} + {% continue %} + {% else %} + {% set ns.continued = false %} + {% endif %} + ( + {% for first_year in range(start_year, end_year + 1) -%} + {% set next_year = first_year + 1 -%} + population_{{ first_year }}_{{ next_year }} + {% if not loop.last -%} + {% endif %} + {% endfor %} + ) as population_{{ start_year }}_{{ end_year + 1 }} + {% if not loop.last and not ns.continued -%}, {% endif %} + {% endfor %} {% if not loop.last and not ns.continued -%}, {% endif %} + {% endfor %} +FROM + flux diff --git a/airflow/include/sql/sparte/models/insee/intersected/conso_pop_communes_of_epci.sql b/airflow/include/sql/sparte/models/insee/intersected/conso_pop_communes_of_epci.sql deleted file mode 100644 index 6e4a6ce12..000000000 --- a/airflow/include/sql/sparte/models/insee/intersected/conso_pop_communes_of_epci.sql +++ /dev/null @@ -1,36 +0,0 @@ -{{ config(materialized='view') }} - - --- WIP -with by_epci as ( - SELECT - array_agg(conso.conso_2011_2021) as communes_consommation, - - commune.epci - FROM - {{ ref('consommation_cog_2024') }} as conso - LEFT JOIN - {{ ref('population')}} as pop - ON - pop.code_commune = conso.commune_code - LEFT JOIN - {{ ref('commune') }} as commune - ON - conso.commune_code = commune.code - GROUP BY - commune.epci -), by_epci_with_median as ( - SELECT - epci, - percentile_cont(0.5) WITHIN GROUP (ORDER BY unnested_conso) as median, - communes_consommation - FROM - by_epci - CROSS JOIN LATERAL unnest(communes_consommation) as unnested_conso - GROUP BY - epci, - communes_consommation -) -SELECT * FROM by_epci_with_median - --- PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY taux_de_consommation) AS median