146 lines
7.1 KiB
SQL
146 lines
7.1 KiB
SQL
{{ config(materialized = 'view') }}
|
|
-- The procedures the notes, orders and biopsies imply but silver doesn't hold as such, in the timeline's shape
|
|
-- (`cacu_linea_de_tiempo` adds them to each patient's timeline):
|
|
-- colposcopia a colposcopy: a colposcopy note (by its reason, its ICD-10 or its diagnosis text), a
|
|
-- biopsy order, or a reported biopsy
|
|
-- tratamiento a note with a treatment ICD-10 after the patient's first colposcopy
|
|
-- citologia_de_vigilancia a cytology whose order's specialty is unknown or one of the surveillance ones
|
|
-- diagnostico a note that can resolve a cytology: a diagnosis ICD-10 (`cie_diagnostico`) written by
|
|
-- a diagnosing specialty (`especialidades_diagnostico`)
|
|
-- antecedente a note with a cervical cancer ICD-10 (`cie_antecedente`), whoever wrote it
|
|
-- Every rule is a dbt var (`seguimiento`, and the diagnosis rules shared with CACU_DIAS_TOMA_DIAGNOSTICO), so
|
|
-- changing one is recompiling, not reloading.
|
|
{% set s = var('seguimiento') %}
|
|
|
|
with nota as (
|
|
select
|
|
p.pac_id,
|
|
p.fecha,
|
|
p.fuente_tabla,
|
|
p.fuente_id,
|
|
c.inicio as conocida_en,
|
|
max(case when o.resultado_id = 'nota_medica.diagnostico' then o.valor_texto end) as cie_codigo,
|
|
max(case when o.resultado_id = 'nota_medica.especialidad' then o.valor_id end) as especialidad,
|
|
max(case when o.resultado_id = 'nota_medica.motivo' then o.valor_texto end) as motivo,
|
|
max(case when o.resultado_id = 'nota_medica.diagnostico_texto' then o.valor_texto end) as diagnostico_texto
|
|
from {{ source('silver', 'procedimiento') }} p
|
|
join {{ source('silver', 'carga') }} c on c.carga_id = p.carga_id
|
|
left join {{ source('silver', 'observacion_clinica') }} o on o.procedimiento_id = p.procedimiento_id
|
|
where p.estudio_id = 'nota_medica' and p.clase = 'encuentro'
|
|
group by p.procedimiento_id, p.pac_id, p.fecha, p.fuente_tabla, p.fuente_id, c.inicio
|
|
),
|
|
|
|
toma as (
|
|
select t.pac_id, t.fecha, t.fuente_tabla, t.fuente_id, c.inicio as conocida_en, o.valor_id as especialidad
|
|
from {{ source('silver', 'procedimiento') }} t
|
|
join {{ source('silver', 'carga') }} c on c.carga_id = t.carga_id
|
|
left join {{ source('silver', 'procedimiento') }} orden
|
|
on orden.procedimiento_id = t.padre_id and orden.estudio_id = 'citologia' and orden.clase = 'orden'
|
|
left join {{ source('silver', 'observacion_clinica') }} o
|
|
on o.procedimiento_id = orden.procedimiento_id and o.resultado_id = 'orden.especialidad'
|
|
where t.estudio_id = 'citologia' and t.clase = 'toma'
|
|
),
|
|
|
|
diagnostico as (
|
|
select * from nota
|
|
where cie_codigo in {{ sql_list(var('cie_diagnostico')) }}
|
|
and especialidad in {{ sql_list(var('especialidades_diagnostico')) }}
|
|
),
|
|
|
|
-- The valuation visit: the first diagnosis that resolves one of her cytologies, after it and within the horizon,
|
|
-- as CACU_DIAS_TOMA_DIAGNOSTICO reads it. Days are differences of `dim_fecha.dia_num`.
|
|
valoracion as (
|
|
select n.pac_id, min(n.fecha) as primera
|
|
from diagnostico n
|
|
join toma t on t.pac_id = n.pac_id
|
|
join {{ source('silver', 'dim_fecha') }} dn on dn.fecha = n.fecha
|
|
join {{ source('silver', 'dim_fecha') }} dt on dt.fecha = t.fecha
|
|
where n.fecha > t.fecha
|
|
and dn.dia_num - dt.dia_num < {{ var('horizonte_toma_diagnostico_dias') }}
|
|
group by n.pac_id
|
|
),
|
|
|
|
-- A colposcopy note: of a colposcopy specialty and, by rule, after the first valuation visit.
|
|
nota_de_colposcopia as (
|
|
select n.*
|
|
from nota n
|
|
{% if s['colposcopia_requisito'] == 'valoracion_previa' %}
|
|
join valoracion v on v.pac_id = n.pac_id and n.fecha > v.primera
|
|
{% endif %}
|
|
where n.especialidad in {{ sql_list(s['colposcopia_especialidades']) }}
|
|
),
|
|
|
|
-- What counts as a colposcopy. A biopsy order, and a reported biopsy's request, count by rule only on the day of a
|
|
-- colposcopy note: the colposcopy is the biopsy's parent.
|
|
colposcopia as (
|
|
select pac_id, fecha, conocida_en, fuente_tabla, fuente_id from nota_de_colposcopia where 1 = 0
|
|
{% if 'motivo' in s['colposcopia_evidencias'] %}{% for m in s['colposcopia_motivos'] %}
|
|
union all
|
|
select pac_id, fecha, conocida_en, fuente_tabla, fuente_id from nota_de_colposcopia where {{ sin_acentos_mayusculas('motivo') }} like '%{{ m }}%'
|
|
{% endfor %}{% endif %}
|
|
{% if 'cie' in s['colposcopia_evidencias'] %}
|
|
union all
|
|
select pac_id, fecha, conocida_en, fuente_tabla, fuente_id from nota_de_colposcopia where cie_codigo in {{ sql_list(s['colposcopia_cie']) }}
|
|
{% endif %}
|
|
{% if 'diagnostico_texto' in s['colposcopia_evidencias'] %}{% for d in s['colposcopia_diagnosticos'] %}
|
|
union all
|
|
select pac_id, fecha, conocida_en, fuente_tabla, fuente_id from nota_de_colposcopia
|
|
where {{ sin_acentos_mayusculas('diagnostico_texto') }} like '%{{ d }}%'
|
|
{% endfor %}{% endif %}
|
|
{% if 'orden_biopsia' in s['colposcopia_evidencias'] %}
|
|
union all
|
|
select o.pac_id, o.fecha, c.inicio, o.fuente_tabla, o.fuente_id
|
|
from {{ source('silver', 'procedimiento') }} o
|
|
join {{ source('silver', 'carga') }} c on c.carga_id = o.carga_id
|
|
where o.estudio_id = 'biopsia' and o.clase = 'orden'
|
|
{% if s['biopsia_requisito'] == 'mismo_dia_que_colposcopia' %}
|
|
and exists (select 1 from nota_de_colposcopia v where v.pac_id = o.pac_id and v.fecha = o.fecha)
|
|
{% endif %}
|
|
{% endif %}
|
|
{% if 'biopsia' in s['colposcopia_evidencias'] %}
|
|
union all
|
|
select b.pac_id, b.fecha_reporte, c.inicio, b.fuente_tabla, b.fuente_id
|
|
from {{ ref('cacu_biopsias') }} b
|
|
join {{ source('silver', 'carga') }} c on c.carga_id = b.reporte_carga_id
|
|
where 1 = 1
|
|
{% if s['biopsia_requisito'] == 'mismo_dia_que_colposcopia' %}
|
|
and exists (select 1 from nota_de_colposcopia v where v.pac_id = b.pac_id and v.fecha = b.fecha_solicitud)
|
|
{% endif %}
|
|
{% endif %}
|
|
),
|
|
|
|
primera_colposcopia as (
|
|
select pac_id, min(fecha) as primera from colposcopia group by pac_id
|
|
),
|
|
|
|
inferidos as (
|
|
select pac_id, fecha, conocida_en, 'colposcopia' as tipo, fuente_tabla, fuente_id from colposcopia
|
|
union all
|
|
select n.pac_id, n.fecha, n.conocida_en, 'tratamiento', n.fuente_tabla, n.fuente_id
|
|
from nota n
|
|
join primera_colposcopia pc on pc.pac_id = n.pac_id and n.fecha > pc.primera
|
|
where n.cie_codigo in {{ sql_list(s['tratamiento_cie']) }}
|
|
union all
|
|
select pac_id, fecha, conocida_en, 'diagnostico', fuente_tabla, fuente_id from diagnostico
|
|
union all
|
|
select pac_id, fecha, conocida_en, 'antecedente', fuente_tabla, fuente_id from nota
|
|
where cie_codigo in {{ sql_list(var('cie_antecedente')) }}
|
|
union all
|
|
select pac_id, fecha, conocida_en, 'citologia_de_vigilancia', fuente_tabla, fuente_id from toma
|
|
where especialidad is null or especialidad in {{ sql_list(s['vigilancia_especialidades']) }}
|
|
)
|
|
|
|
select
|
|
pac_id,
|
|
fecha,
|
|
conocida_en,
|
|
{{ as_text("'procedimiento'") }} as clase,
|
|
{{ as_text('tipo') }} as tipo,
|
|
{{ as_text('tipo') }} as codigo,
|
|
{{ as_text('null') }} as valor_codigo,
|
|
cast(null as {{ dbt.type_float() }}) as valor_numerico,
|
|
fuente_tabla,
|
|
fuente_id
|
|
from inferidos
|
|
where fecha is not null
|