Files
ISSSTELEON/Transformation/models/cama/silver/cama_linea_de_tiempo.sql
T

27 lines
1.6 KiB
SQL

{# A view, not ephemeral like the other silver models: cohorts query it. Its sources: cama_linea_de_tiempo.yml;
the procedures silver doesn't hold but its notes imply (diagnosis, antecedent) and the clinical scenario of each
patient: cama_procedimientos_inferidos; her birth and death, from `persona`. #}
{{ config(materialized = 'view') }}
{{ linea_de_tiempo(config.get('meta')['fuentes']) }}
union all
select pac_id, fecha, conocida_en, clase, tipo, codigo, valor_codigo, valor_numerico, fuente_tabla, fuente_id
from {{ ref('cama_procedimientos_inferidos') }}
{#- `persona` holds staging's current value, with no load of its own: it's known as of the latest load. #}
{%- for tipo, columna in [('nacimiento', 'fecha_nacimiento'), ('defuncion', 'fecha_defuncion')] %}
union all
select
persona.pac_id as pac_id,
persona.{{ columna }} as fecha,
(select max(carga.inicio) from {{ source('silver', 'carga') }} carga) as conocida_en,
{{ as_text("'persona'") }} as clase,
{{ as_text("'" ~ tipo ~ "'") }} as tipo,
{{ as_text("'" ~ tipo ~ "'") }} as codigo,
{{ as_text('null') }} as valor_codigo,
cast(null as {{ dbt.type_float() }}) as valor_numerico,
{{ as_text("'persona'") }} as fuente_tabla,
{{ as_text('persona.pac_id') }} as fuente_id
from {{ source('silver', 'persona') }} persona
where persona.{{ columna }} is not null
{%- endfor %}