Files
ISSSTELEON/Persistance/dbt/models/gold/cacu/cacu_events.sql
T
alberto 781e1ea75a C1a: code only, no comments, no Markdown
The four Markdown files (README.md, tests/fixtures/ados2_items_by_module.md, tools/delivery/README.md, tools/delivery/README.repo.md) live in the outer repo under docs/ISSSTELEON; the delivery scripts no longer produce a README and the ADOS-2 expected values stay in the Python test.
2026-12-01 16:56:14 -06:00

128 lines
4.9 KiB
SQL

{% set p = var('cacu') %}
with cytology as (
select * from {{ ref('cervical_cytology') }}
),
biopsy as (
select * from {{ ref('cervical_biopsy') }}
),
note as (
select
person_id, note_date, specialty, known_at,
replace(icd10, '.', '') as icd10,
{{ plain_upper('reason') }} as reason,
{{ plain_upper('diagnosis_text') }} as diagnosis_text,
{{ dbt.concat(['note_date', "'|'", "coalesce(specialty, '')"]) }} as source_id
from {{ ref('clinical_note') }}
),
diagnosis as (
select * from note
where icd10 in {{ sql_list(p.diagnosis_icd10) }}
and specialty in {{ sql_list(p.diagnosis_specialties) }}
),
assessed as (
select n.person_id, min(n.note_date) as first_date
from diagnosis as n
join cytology as c on c.person_id = n.person_id
where n.note_date > c.sample_date
and {{ days_between('c.sample_date', 'n.note_date') }} < {{ p.sample_to_diagnosis_days }}
group by n.person_id
),
colposcopy_clinic as (
select n.*
from note as n
join assessed as a on a.person_id = n.person_id and n.note_date > a.first_date
where n.specialty in {{ sql_list(p.colposcopy_specialties) }}
),
colposcopy as (
select person_id, note_date as date, known_at, 'clinical_note' as source_table, source_id
from colposcopy_clinic
where icd10 in {{ sql_list(p.colposcopy_icd10) }}
{%- for r in p.colposcopy_reasons %}
or reason like '%{{ r }}%'
{%- endfor %}
{%- for d in p.colposcopy_diagnoses %}
or diagnosis_text like '%{{ d }}%'
{%- endfor %}
union
select b.person_id, b.request_date, b.known_at, 'cervical_biopsy', cast(b.request_date as {{ dbt.type_string() }})
from biopsy as b
where exists (select 1 from colposcopy_clinic as v
where v.person_id = b.person_id and v.note_date = b.request_date)
union
select b.person_id, b.report_date, b.known_at, 'cervical_biopsy', cast(b.request_date as {{ dbt.type_string() }})
from biopsy as b
where b.report_date is not null
and exists (select 1 from colposcopy_clinic as v
where v.person_id = b.person_id and v.note_date = b.request_date)
),
first_colposcopy as (
select person_id, min(date) as first_date from colposcopy group by person_id
),
events as (
select person_id, birth_date as date, known_at, 'birth' as type,
cast(null as {{ dbt.type_string() }}) as value_code,
cast(null as {{ dbt.type_float() }}) as value_num,
'person' as source_table, person_id as source_id
from {{ ref('person') }} where birth_date is not null
union all
select person_id, death_date, known_at, 'death', null, null, 'person', person_id
from {{ ref('death') }}
union all
select person_id, sample_date, known_at, 'cytology', adequacy, null,
'cervical_cytology', cast(sample_date as {{ dbt.type_string() }})
from cytology
union all
select person_id, report_date, known_at, 'bethesda', result, null,
'cervical_cytology', cast(sample_date as {{ dbt.type_string() }})
from cytology where result is not null and report_date is not null
union all
select person_id, report_date, known_at, 'hpv',
case when hpv then 'positive' else 'negative' end, null,
'cervical_cytology', cast(sample_date as {{ dbt.type_string() }})
from cytology where hpv is not null and report_date is not null
union all
select person_id, report_date, known_at, 'hpv_16_18', null,
case when hpv_16 or hpv_18 then 1.0 else 0.0 end,
'cervical_cytology', cast(sample_date as {{ dbt.type_string() }})
from cytology where (hpv_16 is not null or hpv_18 is not null) and report_date is not null
union all
select person_id, request_date, known_at, 'biopsy_order', null, null,
'cervical_biopsy', cast(request_date as {{ dbt.type_string() }})
from biopsy
union all
select person_id, report_date, known_at, 'biopsy', histology, null,
'cervical_biopsy', cast(request_date as {{ dbt.type_string() }})
from biopsy where histology is not null and report_date is not null
union all
select person_id, note_date, known_at, 'note', icd10, null, 'clinical_note', source_id
from note
union all
select person_id, note_date, known_at, 'diagnosis', null, null, 'clinical_note', source_id
from diagnosis
union all
select person_id, note_date, known_at, 'history', null, null, 'clinical_note', source_id
from note where icd10 in {{ sql_list(p.history_icd10) }}
union all
select n.person_id, n.note_date, n.known_at, 'treatment', null, null, 'clinical_note', n.source_id
from note as n
join first_colposcopy as f on f.person_id = n.person_id and n.note_date > f.first_date
where n.icd10 in {{ sql_list(p.treatment_icd10) }}
union all
select person_id, date, known_at, 'colposcopy', null, null, source_table, source_id
from colposcopy
)
select * from events where date is not null