diff --git a/airflow/include/sql/sparte/macros/cumulative_flux.sql b/airflow/include/sql/sparte/macros/cumulative_flux.sql new file mode 100644 index 000000000..2d51684c4 --- /dev/null +++ b/airflow/include/sql/sparte/macros/cumulative_flux.sql @@ -0,0 +1,18 @@ +{% macro cumulative_flux(first_available_year, last_available_year) %} + {% set ns = namespace(continued=false) %} + {% for start_year in range(first_available_year, last_available_year + 1) %} + {% for end_year in range(first_available_year, last_available_year + 1) -%} + {% if start_year > end_year -%} + {% set ns.continued = true %} + {% continue %} + {% else %} + {% set ns.continued = false %} + {% endif %} + {{ caller( + start_year=start_year, + end_year=end_year, + )}} + {% if not loop.last and not ns.continued -%}, {% endif %} + {% endfor %} {% if not loop.last and not ns.continued -%}, {% endif %} + {% endfor %} +{% endmacro %} 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 index e56401cda..a59d81466 100644 --- 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 @@ -4,22 +4,13 @@ ) %} 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 %} + {% call(start_year, end_year) cumulative_flux( + first_available_year=2009, + last_available_year=2020 + ) %} + sum(population_{{ start_year }}_{{ end_year + 1 }}) + as population_{{ start_year }}_{{ end_year + 1 }} + {% endcall %} FROM {{ ref('flux_population') }} as flux_population LEFT JOIN diff --git a/airflow/include/sql/sparte/models/insee/flux_population.sql b/airflow/include/sql/sparte/models/insee/flux_population.sql index 28a7f4eec..e6c50945a 100644 --- a/airflow/include/sql/sparte/models/insee/flux_population.sql +++ b/airflow/include/sql/sparte/models/insee/flux_population.sql @@ -24,26 +24,17 @@ with flux as ( ) 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 %} + {% call(start_year, end_year) cumulative_flux( + first_available_year=2009, + last_available_year=2020 + ) %} + ( + {% 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 }} + {% endcall %} FROM flux