Files
ISSSTELEON/Transformation/models/cacu/gold/eventos/cacu_tomas_biopsia.sql
T

42 lines
1.8 KiB
SQL

-- One row per cytology taken and each biopsy report it led to (one row with nulls when it led to none): the event
-- grain of the measures that follow a sample to its biopsy. When the sample and the report became known are the
-- start of the loads that wrote them. The birth date is the patient's, for strata by age. The sample's own result
-- (with its quality and when it was known) lets a measure pick a patient's last valid sample, and `biopsia_gravedad`
-- orders her biopsies from least to most severe by `catalogo_biopsia`: 0 for a grade the catalog doesn't know, -1 for
-- a report without one.
select
c.procedimiento_id,
c.fuente_id,
c.pac_id,
c.fecha_toma,
toma.inicio as toma_conocida_en,
c.resultado,
c.calidad_muestra,
c.fecha_reporte as citologia_fecha_reporte,
resultado.inicio as resultado_conocido_en,
b.fuente_id as biopsia_fuente_id,
b.fecha_reporte as biopsia_fecha_reporte,
b.grado as biopsia_grado,
case
when b.procedimiento_id is null then null
when b.grado is null then -1
{%- for grado in var('catalogo_biopsia') %}
when b.grado = '{{ grado }}' then {{ loop.index }}
{%- endfor %}
else 0
end as biopsia_gravedad,
reporte.inicio as biopsia_conocida_en,
persona.fecha_nacimiento
from {{ ref('cacu_citologias') }} c
join {{ source('silver', 'carga') }} toma
on toma.carga_id = c.toma_carga_id
join {{ source('silver', 'persona') }} persona
on persona.pac_id = c.pac_id
left join {{ source('silver', 'carga') }} resultado
on resultado.carga_id = c.resultado_carga_id and c.resultado is not null
left join {{ ref('cacu_biopsias') }} b
on b.toma_id = c.procedimiento_id and b.pac_id = c.pac_id
left join {{ source('silver', 'carga') }} reporte
on reporte.carga_id = b.reporte_carga_id