94 lines
4.8 KiB
SQL
94 lines
4.8 KiB
SQL
{#
|
|
Each patient's timeline, from silver's procedimiento and observacion_clinica: one UNION ALL branch per source
|
|
(a domain's `fuentes`, declared in its timeline model's YAML). Columns: pac_id, fecha, conocida_en, clase, tipo,
|
|
codigo, valor_codigo, valor_numerico, fuente_tabla, fuente_id.
|
|
|
|
Per source:
|
|
- the procedures of its `clase` (and `estudio_id`, unless null: an order of any study);
|
|
- its value: the observation of `resultado_id`, read by the result's type in `cat_resultado` (a code is
|
|
coalesce(valor_id, valor_fuente)), or a number with `valor: numerico`; without a result, the procedure's
|
|
`columna`, trimmed;
|
|
- its date and patient: the procedure's, or its parent's (`del_padre`); an observed date (`fecha_resultado`)
|
|
goes first; `fecha: nacimiento` takes the person's birth date;
|
|
- `conocida_en`: the start of the load that wrote the row (the observation's, or else the procedure's);
|
|
- rows without a date are left out, and so are measurements, diagnoses and history without a value.
|
|
#}
|
|
{% macro linea_de_tiempo(fuentes) -%}
|
|
select pac_id, fecha, conocida_en, clase, tipo, codigo, valor_codigo, valor_numerico, fuente_tabla, fuente_id
|
|
from (
|
|
{%- for f in fuentes %}
|
|
{% if not loop.first %}union all{% endif %}
|
|
{{ linea_de_tiempo_fuente(f) }}
|
|
{%- endfor %}
|
|
) linea
|
|
{%- endmacro %}
|
|
|
|
|
|
{% macro linea_de_tiempo_fuente(f) -%}
|
|
{%- set resultado = f.get('resultado_id') -%}
|
|
{%- set con_fecha = 'padre' if f.get('del_padre') else 'p' -%}
|
|
{%- set fecha = con_fecha ~ '.fecha' -%}
|
|
{%- if f.get('fecha_resultado') -%}{%- set fecha = 'coalesce(fecha_observada.valor_fecha, ' ~ fecha ~ ')' -%}{%- endif -%}
|
|
{%- if f.get('fecha') == 'nacimiento' -%}{%- set fecha = 'persona.fecha_nacimiento' -%}{%- endif -%}
|
|
{%- if resultado and f.get('valor') == 'numerico' -%}
|
|
{%- set valor_codigo = as_text('null') -%}
|
|
{%- set valor_numerico = 'cast(o.valor_numerico as ' ~ dbt.type_float() ~ ')' -%}
|
|
{%- elif resultado -%}
|
|
{%- set valor_codigo = as_text(
|
|
"case r.tipo_valor"
|
|
~ " when 'codigo' then coalesce(o.valor_id, o.valor_fuente)"
|
|
~ " when 'booleano' then " ~ as_integer('o.valor_numerico')
|
|
~ " when 'numero' then o.valor_numerico"
|
|
~ " when 'fecha' then o.valor_fecha"
|
|
~ " else o.valor_texto end") -%}
|
|
{%- set valor_numerico = 'cast(null as ' ~ dbt.type_float() ~ ')' -%}
|
|
{%- elif f.get('columna') -%}
|
|
{%- set valor_codigo = "nullif(trim(p." ~ f['columna'] ~ "), '')" -%}
|
|
{%- set valor_numerico = 'cast(null as ' ~ dbt.type_float() ~ ')' -%}
|
|
{%- else -%}
|
|
{%- set valor_codigo = as_text('null') -%}
|
|
{%- set valor_numerico = 'cast(null as ' ~ dbt.type_float() ~ ')' -%}
|
|
{%- endif %}
|
|
select
|
|
{{ con_fecha }}.pac_id as pac_id,
|
|
{{ fecha }} as fecha,
|
|
carga.inicio as conocida_en,
|
|
{{ as_text("'" ~ f['clase_de_linea'] ~ "'") }} as clase,
|
|
{{ as_text("'" ~ f['tipo'] ~ "'") }} as tipo,
|
|
{{ as_text("'" ~ (f.get('codigo') or f['tipo']) ~ "'") }} as codigo,
|
|
{{ valor_codigo }} as valor_codigo,
|
|
{{ valor_numerico }} as valor_numerico,
|
|
p.fuente_tabla as fuente_tabla,
|
|
p.fuente_id as fuente_id
|
|
from {{ source('silver', 'procedimiento') }} p
|
|
{%- if resultado %}
|
|
left join {{ source('silver', 'observacion_clinica') }} o
|
|
on o.procedimiento_id = p.procedimiento_id and o.resultado_id = '{{ resultado }}'
|
|
left join {{ source('silver', 'cat_resultado') }} r
|
|
on r.resultado_id = '{{ resultado }}'
|
|
{%- endif %}
|
|
{%- if f.get('del_padre') %}
|
|
join {{ source('silver', 'procedimiento') }} padre
|
|
on padre.procedimiento_id = p.padre_id
|
|
{%- endif %}
|
|
{%- if f.get('fecha_resultado') %}
|
|
left join {{ source('silver', 'observacion_clinica') }} fecha_observada
|
|
on fecha_observada.procedimiento_id = {{ con_fecha }}.procedimiento_id
|
|
and fecha_observada.resultado_id = '{{ f['fecha_resultado'] }}'
|
|
{%- endif %}
|
|
{%- if f.get('fecha') == 'nacimiento' %}
|
|
join {{ source('silver', 'persona') }} persona
|
|
on persona.pac_id = {{ con_fecha }}.pac_id
|
|
{%- endif %}
|
|
join {{ source('silver', 'carga') }} carga
|
|
on carga.carga_id = {{ 'coalesce(o.carga_id, p.carga_id)' if resultado else 'p.carga_id' }}
|
|
where p.clase = '{{ f['clase'] }}'
|
|
and {{ fecha }} is not null
|
|
{%- if f.get('estudio_id') %}
|
|
and p.estudio_id = '{{ f['estudio_id'] }}'
|
|
{%- endif %}
|
|
{%- if f['clase_de_linea'] in ['medicion', 'diagnostico', 'antecedente'] %}
|
|
and ({{ valor_codigo }} is not null or {{ valor_numerico }} is not null)
|
|
{%- endif %}
|
|
{%- endmacro %}
|