Files
ISSSTELEON/Transformation/models/cacu/gold/eventos/cacu_riesgo_vigilancia.sql
T
edgar 3f1925b775 Each risk keeps the model's index date for its patient
evaluacion_riesgo.fecha_indice: the decision point the model scored the patient from, which the horizons count from
(the model defines it, not the cohort member). Migrations for CaCu, CaMa and TEA add it, fill existing risks with their
member's index date in the batch's build, and make it required. save_batch takes (pac_id, fecha_indice, horizonte,
probabilidad, ic_bajo, ic_alto) and refuses a risk with no date or two dates for one patient; change_state keeps it;
current_risk returns it and the batch's outcome. The risk-band measure dates each row by the estimate's index date.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HmQxVJVNR5FYkPAregGEQ7
2026-11-20 23:02:45 -06:00

51 lines
2.3 KiB
SQL

-- One row per member of the surveillance cohort build that the current batch of risk estimates scored, with the
-- index date of her estimate (`evaluacion_riesgo.fecha_indice`: the model's decision point, which dates the period)
-- and the probability of her estimate in force, null if she has none. A member the batch never scored keeps her
-- member's index date, so she still falls in a period as no data. The current batch is the latest one
-- of the cohort with some estimate in force; an estimate is in force when its latest version, at the horizon
-- `riesgos_horizonte_banda`, is `final` (a later `entered-in-error` version voids it). When a member and an estimate
-- became known: the build's and the estimate's registration.
with recientes as (
select
e.lote_id,
e.pac_id,
e.fecha_indice,
e.probabilidad,
e.estado,
e.registrado_en,
row_number() over (partition by e.lote_id, e.pac_id order by e.version desc) as lugar
from {{ source('oro', 'evaluacion_riesgo') }} e
where e.horizonte_anios = {{ var('riesgos_horizonte_banda') }}
),
vigentes as (
select lote_id, pac_id, probabilidad, registrado_en from recientes where lugar = 1 and estado = 'final'
),
-- the index date of each scored member's estimate, whatever its state (a voided one still dates her)
fechas as (
select lote_id, pac_id, max(fecha_indice) as fecha_indice from recientes group by lote_id, pac_id
),
lote_vigente as (
select max(l.lote_id) as lote_id
from {{ source('oro', 'evaluacion_riesgo_lote') }} l
join {{ source('oro', 'cohorte_armado') }} a on a.armado_id = l.armado_id
where a.url = 'cacu/cohorte_vigilancia'
and l.lote_id in (select lote_id from vigentes)
)
select
m.pac_id,
coalesce(f.fecha_indice, m.fecha_indice) as fecha_indice,
a.armado_en as miembro_conocido_en,
v.probabilidad,
v.registrado_en as evaluacion_conocida_en
from lote_vigente lv
join {{ source('oro', 'evaluacion_riesgo_lote') }} l on l.lote_id = lv.lote_id
join {{ source('oro', 'cohorte_armado') }} a on a.armado_id = l.armado_id
join {{ source('oro', 'cohorte_miembro') }} m on m.armado_id = l.armado_id
left join vigentes v on v.lote_id = lv.lote_id and v.pac_id = m.pac_id
left join fechas f on f.lote_id = lv.lote_id and f.pac_id = m.pac_id