51 lines
2.1 KiB
SQL
51 lines
2.1 KiB
SQL
/* One row per EEG and metric its analysis measured: the value, the child's age group at the EEG, and the analysis's
|
|
comparison with the metric's age baseline: its z and the baseline's p5, median and p95 at the child's age
|
|
(`ref_min`, `ref_mediana`, `ref_max`). The state comes from the analysis's -1/0/+1: `aumentado` above p95,
|
|
`disminuido` below p5, `normal` within; `no_evaluable` without a z (no value, no baseline in production, or an age
|
|
the baseline doesn't cover). */
|
|
|
|
{%- set deviation = ['z', 'fuera', 'p5', 'mediana', 'p95'] %}
|
|
|
|
with medidas as (
|
|
{%- for m in tea_metricas() %}
|
|
select
|
|
e.procedimiento_id,
|
|
e.pac_id,
|
|
e.fecha_eeg,
|
|
{{ as_text("'" ~ m ~ "'") }} as metrica,
|
|
max(case when o.resultado_id = 'eeg.{{ m }}' then o.valor_numerico end) as valor
|
|
{%- for d in deviation %},
|
|
max(case when o.resultado_id = 'eeg.{{ m }}_{{ d }}' then o.valor_numerico end) as {{ d }}
|
|
{%- endfor %}
|
|
from {{ ref('tea_eegs') }} e
|
|
join {{ source('silver', 'observacion_clinica') }} o
|
|
on o.procedimiento_id = e.procedimiento_id
|
|
and o.resultado_id in ('eeg.{{ m }}'{% for d in deviation %}, 'eeg.{{ m }}_{{ d }}'{% endfor %})
|
|
group by e.procedimiento_id, e.pac_id, e.fecha_eeg
|
|
having max(case when o.resultado_id = 'eeg.{{ m }}' then 1 end) = 1
|
|
{{ 'union all' if not loop.last }}
|
|
{%- endfor %}
|
|
)
|
|
|
|
select
|
|
m.procedimiento_id,
|
|
m.pac_id,
|
|
m.fecha_eeg,
|
|
m.metrica,
|
|
m.valor,
|
|
{{ tea_grupo_edad(tea_meses_cumplidos('de.dia_num - dn.dia_num')) }} as grupo_edad_eeg,
|
|
m.p5 as ref_min,
|
|
m.mediana as ref_mediana,
|
|
m.p95 as ref_max,
|
|
m.z as valor_z,
|
|
case
|
|
when m.z is null then {{ as_text("'no_evaluable'") }}
|
|
when m.fuera = 1 then {{ as_text("'aumentado'") }}
|
|
when m.fuera = -1 then {{ as_text("'disminuido'") }}
|
|
else {{ as_text("'normal'") }}
|
|
end as estado
|
|
from medidas m
|
|
left join {{ source('silver', 'persona') }} pe on pe.pac_id = m.pac_id
|
|
left join {{ source('silver', 'dim_fecha') }} de on de.fecha = m.fecha_eeg
|
|
left join {{ source('silver', 'dim_fecha') }} dn on dn.fecha = pe.fecha_nacimiento
|