Files
ISSSTELEON/Transformation/models/tea/gold/eventos/tea_alteraciones.sql
T

22 lines
1.5 KiB
SQL

-- One row per child of the cohort with a good EEG and knowledge version (`conocida_en`): the first good EEG known
-- by then (the study's) and, from its
-- metrics, whether it has any alteration and which. Later EEGs, good or bad, don't change them; each alteration
-- counts once per child and a child can have several. What M30 counts, by the period of that EEG.
select
h.pac_id,
h.grupo_edad,
h.eeg_estudio_id as procedimiento_id,
h.fecha_eeg_estudio as fecha_eeg,
h.conocida_en,
max(case when m.estado in ('aumentado', 'disminuido') then 1 else 0 end) as con_alteracion,
max(case when m.metrica = 'ritmo_posterior' and m.estado in ('aumentado', 'disminuido') then 1 else 0 end) as alt_ritmo_posterior,
max(case when m.metrica = 'husos_sueno_n2' and m.estado in ('aumentado', 'disminuido') then 1 else 0 end) as alt_husos_sueno_n2,
max(case when m.metrica = 'husos_sueno_n3' and m.estado in ('aumentado', 'disminuido') then 1 else 0 end) as alt_husos_sueno_n3,
max(case when m.metrica = 'picos_paroxisticos' and m.estado in ('aumentado', 'disminuido') then 1 else 0 end) as alt_picos_paroxisticos,
max(case when m.metrica = 'gradiente_anteroposterior' and m.estado in ('aumentado', 'disminuido') then 1 else 0 end) as alt_gradiente_anteroposterior
from {{ ref('tea_hitos') }} h
left join {{ ref('tea_eeg_metricas') }} m on m.procedimiento_id = h.eeg_estudio_id
where h.eeg_estudio_id is not null
group by h.pac_id, h.grupo_edad, h.eeg_estudio_id, h.fecha_eeg_estudio, h.conocida_en