Skip to content

Commit

Permalink
feat(cumulative_flux): add macro
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisig committed Nov 14, 2024
1 parent c93cd54 commit 997be7b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
18 changes: 18 additions & 0 deletions airflow/include/sql/sparte/macros/cumulative_flux.sql
Original file line number Diff line number Diff line change
@@ -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 %}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 12 additions & 21 deletions airflow/include/sql/sparte/models/insee/flux_population.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 997be7b

Please sign in to comment.