106 lines
4.2 KiB
SQL
106 lines
4.2 KiB
SQL
/*
|
|
One row per child of the cohort and knowledge version (`tea_versiones`): the date of each step of the screening,
|
|
from the facts known by that version. `vigente` marks the child's latest version.
|
|
|
|
Steps go in order (M-CHAT, psychology note with the ADOS-2, neuropediatrics note, good-quality EEG) and each needs
|
|
the previous one: a step is dated by the later of its first fact and the previous step (an EEG before the note
|
|
counts from the note). Without the previous step, a step isn't reached. The neuropediatrics step takes any note;
|
|
`fecha_tea` is the first note that confirms (F84.0), `fecha_sin_tea` the first that doesn't, and both need the
|
|
psychology step, as the note does. `fecha_eeg_estudio` is the first good EEG, the one the study uses, whether or
|
|
not the chain reached it.
|
|
*/
|
|
|
|
{# The first fact of each kind known by each version: (CTE, facts, condition). #}
|
|
{% set primeras = [
|
|
('primera_psicologia', 'ados', '1 = 1'), ('primera_nota', 'notas', '1 = 1'),
|
|
('primera_tea', 'notas', 'h.es_tea = 1'), ('primera_sin_tea', 'notas', 'h.es_tea = 0'),
|
|
('fecha_primer_bueno', 'buenos', '1 = 1')] %}
|
|
|
|
with ados as (
|
|
select a.pac_id, a.fecha_ados2 as fecha, c.inicio as conocida_en
|
|
from {{ ref('tea_ados2') }} a
|
|
join {{ source('silver', 'carga') }} c on c.carga_id = a.carga_id
|
|
where a.fecha_ados2 is not null
|
|
),
|
|
|
|
notas as (
|
|
select n.pac_id, n.fecha_nota as fecha, n.es_tea, c.inicio as conocida_en
|
|
from {{ ref('tea_neuropediatria') }} n
|
|
join {{ source('silver', 'carga') }} c on c.carga_id = n.carga_id
|
|
where n.fecha_nota is not null
|
|
),
|
|
|
|
buenos as (
|
|
select e.pac_id, e.procedimiento_id, e.fecha_eeg as fecha, c.inicio as conocida_en
|
|
from {{ ref('tea_eegs') }} e
|
|
join {{ source('silver', 'carga') }} c on c.carga_id = e.carga_id
|
|
where e.valido = 1 and e.fecha_eeg is not null
|
|
),
|
|
|
|
{% for nombre, hechos, condicion in primeras %}
|
|
{{ nombre }} as (
|
|
select k.pac_id, k.conocida_en, min(h.fecha) as fecha
|
|
from {{ ref('tea_cohorte') }} k
|
|
join {{ hechos }} h on h.pac_id = k.pac_id and h.conocida_en <= k.conocida_en
|
|
where {{ condicion }}
|
|
group by k.pac_id, k.conocida_en
|
|
),
|
|
{% endfor %}
|
|
|
|
|
|
primer_bueno as (
|
|
-- The first good EEG; two on the same day: the first written.
|
|
select f.pac_id, f.conocida_en, f.fecha, min(b.procedimiento_id) as procedimiento_id
|
|
from fecha_primer_bueno f
|
|
join buenos b on b.pac_id = f.pac_id and b.fecha = f.fecha and b.conocida_en <= f.conocida_en
|
|
group by f.pac_id, f.conocida_en, f.fecha
|
|
),
|
|
|
|
encadenados as (
|
|
select
|
|
k.pac_id,
|
|
k.conocida_en,
|
|
k.vigente,
|
|
k.grupo_edad,
|
|
k.fecha_mchat,
|
|
k.mchat_dia,
|
|
{{ tea_despues('k.fecha_mchat', 'ps.fecha') }} as fecha_psicologia,
|
|
nt.fecha as nota_fecha,
|
|
tea.fecha as tea_fecha,
|
|
st.fecha as sin_tea_fecha,
|
|
pb.procedimiento_id as eeg_estudio_id,
|
|
pb.fecha as fecha_eeg_estudio
|
|
from {{ ref('tea_cohorte') }} k
|
|
left join primera_psicologia ps on ps.pac_id = k.pac_id and ps.conocida_en = k.conocida_en
|
|
left join primera_nota nt on nt.pac_id = k.pac_id and nt.conocida_en = k.conocida_en
|
|
left join primera_tea tea on tea.pac_id = k.pac_id and tea.conocida_en = k.conocida_en
|
|
left join primera_sin_tea st on st.pac_id = k.pac_id and st.conocida_en = k.conocida_en
|
|
left join primer_bueno pb on pb.pac_id = k.pac_id and pb.conocida_en = k.conocida_en
|
|
where k.en_cohorte = 1
|
|
),
|
|
|
|
neuro as (
|
|
select
|
|
e.*,
|
|
{{ tea_despues('e.fecha_psicologia', 'e.nota_fecha') }} as fecha_neuropediatria,
|
|
{{ tea_despues('e.fecha_psicologia', 'e.tea_fecha') }} as fecha_tea,
|
|
{{ tea_despues('e.fecha_psicologia', 'e.sin_tea_fecha') }} as fecha_sin_tea
|
|
from encadenados e
|
|
)
|
|
|
|
select
|
|
n.pac_id,
|
|
n.conocida_en,
|
|
n.vigente,
|
|
n.grupo_edad,
|
|
n.fecha_mchat,
|
|
n.mchat_dia,
|
|
n.fecha_psicologia,
|
|
n.fecha_neuropediatria,
|
|
{{ tea_despues('n.fecha_neuropediatria', 'n.fecha_eeg_estudio') }} as fecha_eeg,
|
|
n.fecha_tea,
|
|
n.fecha_sin_tea,
|
|
n.eeg_estudio_id,
|
|
n.fecha_eeg_estudio
|
|
from neuro n
|