Files
ISSSTELEON/Transformation/models/cama/gold/eventos/cama_resultado_diagnostico.sql
T
Angel Capetillo Zavala 9903e22c2a Add CaMa cohort and L3 measures with integrated API and dashboard
Publish deterministic cohort candidates and detection, result-to-diagnosis and BI-RADS histopathology measures. Pin locally prepared WebApi and WebApp commits based on current dev; retain models and shared persistence changes from upstream. Document compatibility, required views and stored-report behavior.
2026-11-17 10:44:08 -06:00

74 lines
3.4 KiB
SQL

-- One row per mammogram that has a report: the grain of the time from the mammogram's result to the diagnosis. No
-- period and no cut-off here: they're the question's. The diagnosis is the first diagnosing note of the patient dated
-- on or after the report (a `diagnostico` of the patient's timeline: a diagnosis ICD-10 of `cama.cie_diagnostico`
-- written by a specialty of `cama.especialidades_diagnostico`) and within the horizon of the report. It is a note of
-- the patient, not a link to this mammogram's episode: the notes carry no episode. It is not the date of a biopsy.
-- Days are differences of `dim_fecha.dia_num`. When the report and the note became known: the start of the loads that
-- wrote them. A breast cancer note (`antecedente`) before the mammogram leaves it out, as in the time from the
-- mammogram to the diagnosis.
{% set r = var('cama') %}
-- One observation per mammogram: its earliest dated report, ties by the report's platform identity.
-- Later corrections do not move the observation to another period or duplicate it.
with ordenadas as (
select procedimiento_id, fuente_id, pac_id, fecha_toma, fecha_reporte, reporte_carga_id,
row_number() over (partition by procedimiento_id order by fecha_reporte, reporte_id, reporte_carga_id) as lugar
from {{ ref('cama_mastografias') }}
where reporte_id is not null and fecha_reporte is not null
),
reportada as (
select procedimiento_id, fuente_id, pac_id, fecha_toma, fecha_reporte, reporte_carga_id
from ordenadas where lugar = 1
),
resuelve as (
select m.procedimiento_id, min(l.fecha) as fecha_diagnostico
from reportada m
join {{ ref('cama_linea_de_tiempo') }} l
on l.pac_id = m.pac_id and l.tipo = 'diagnostico' and l.fecha >= m.fecha_reporte
join {{ source('silver', 'dim_fecha') }} dl on dl.fecha = l.fecha
join {{ source('silver', 'dim_fecha') }} dr on dr.fecha = m.fecha_reporte
where dl.dia_num - dr.dia_num < {{ r['horizonte_toma_diagnostico_dias'] }}
group by m.procedimiento_id
),
diagnostico as (
select r.procedimiento_id, r.fecha_diagnostico, min(l.conocida_en) as diagnostico_conocido_en
from resuelve r
join reportada m on m.procedimiento_id = r.procedimiento_id
join {{ ref('cama_linea_de_tiempo') }} l
on l.pac_id = m.pac_id and l.tipo = 'diagnostico' and l.fecha = r.fecha_diagnostico
group by r.procedimiento_id, r.fecha_diagnostico
),
primer_antecedente as (
select pac_id, min(fecha) as fecha
from {{ ref('cama_linea_de_tiempo') }}
where tipo = 'antecedente'
group by pac_id
)
select
m.procedimiento_id,
m.fuente_id,
m.pac_id,
m.fecha_toma,
m.fecha_reporte,
reporte.inicio as reporte_conocido_en,
dia_reporte.dia_num as reporte_dia,
dx.fecha_diagnostico,
dx.diagnostico_conocido_en,
dia_diagnostico.dia_num - dia_reporte.dia_num as dias_reporte_diagnostico,
case when antecedente.fecha < m.fecha_toma then 1 else 0 end as con_antecedente
from reportada m
join {{ source('silver', 'carga') }} reporte
on reporte.carga_id = m.reporte_carga_id
join {{ source('silver', 'dim_fecha') }} dia_reporte
on dia_reporte.fecha = m.fecha_reporte
left join diagnostico dx
on dx.procedimiento_id = m.procedimiento_id
left join {{ source('silver', 'dim_fecha') }} dia_diagnostico
on dia_diagnostico.fecha = dx.fecha_diagnostico
left join primer_antecedente antecedente
on antecedente.pac_id = m.pac_id