Files
ISSSTELEON/Transformation/macros/tea.sql
T

45 lines
2.0 KiB
SQL

{#
Completed months from a number of days, as TEA counts them: days / `tea.dias_por_mes`, rounded to one decimal,
then truncated (a child of 26.96 months is 27.0 and so 27, one of 26.94 is 26). Days are differences of
`dim_fecha.dia_num`, never negative here.
Truncation is `floor`, except on SQLite, where a cast to integer truncates (PostgreSQL and MySQL round on that
cast). PostgreSQL only rounds a numeric to a number of decimals, hence the cast to decimal first.
#}
{% macro tea_meses_cumplidos(dias) -%}
{{ return(adapter.dispatch('tea_meses_cumplidos', 'isssteleon')(dias)) }}
{%- endmacro %}
{% macro default__tea_meses_cumplidos(dias) -%}
{{ as_integer('floor(round(cast(' ~ dias ~ ' as decimal(12, 4)) / ' ~ var('tea')['dias_por_mes'] ~ ', 1))') }}
{%- endmacro %}
{% macro sqlite__tea_meses_cumplidos(dias) -%}
cast(round(({{ dias }}) / {{ var('tea')['dias_por_mes'] }}, 1) as integer)
{%- endmacro %}
{#
The age group of a number of completed months: the `tea.ventanas` window it falls in, `sin_dato` when the months
are null (no birth date), and null when it falls outside every window.
#}
{% macro tea_grupo_edad(meses) -%}
case
when {{ meses }} is null then {{ as_text("'sin_dato'") }}
{%- for v in var('tea')['ventanas'] %}
when {{ meses }} between {{ v['desde'] }} and {{ v['hasta'] }} then {{ as_text("'" ~ v['clave'] ~ "'") }}
{%- endfor %}
end
{%- endmacro %}
{# A bounded number of the EEG's quality: within its threshold of `tea.calidad`. #}
{% macro tea_criterio_bueno(columna, umbral) -%}
({{ columna }} is not null and {{ columna }} <= {{ var('tea')['calidad'][umbral] }})
{%- endmacro %}
{# The later of two values when both are known, else null: a step reached after the previous one is dated by the
later of its fact and that step. SQL Server 2016 has no GREATEST. #}
{% macro tea_despues(previo, propio) -%}
case when ({{ previo }}) is null or ({{ propio }}) is null then null
when ({{ propio }}) > ({{ previo }}) then ({{ propio }}) else ({{ previo }}) end
{%- endmacro %}