Skip to content

Commit b1eaced

Browse files
author
Alexandra Pavlyshina
committed
measure-evaluate: backfill legacy views with columns required by SoF-era measure SQL
1 parent 863a23f commit b1eaced

1 file changed

Lines changed: 48 additions & 4 deletions

File tree

  • aidbox-custom-operations/measure-evaluate/sql/legacy

aidbox-custom-operations/measure-evaluate/sql/legacy/01-views.sql

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ SELECT
7474
r.resource->'type'->0->'coding'->0->>'system' AS type_system,
7575
r.resource->'type'->0->'coding'->0->>'code' AS type_code,
7676
parse_fhir_datetime(r.resource->'period'->>'start') AS period_start,
77-
parse_fhir_datetime(r.resource->'period'->>'end') AS period_end
77+
parse_fhir_datetime(r.resource->'period'->>'end') AS period_end,
78+
-- Encounter.class.code — used by CMS165/CMS143 for inpatient/ED filtering (EMER, IMP, ACUTE, etc.)
79+
r.resource->'class'->>'code' AS class_code,
80+
-- Encounter.hospitalization.dischargeDisposition — used by shared_hospice
81+
r.resource->'hospitalization'->'dischargeDisposition'->'coding'->0->>'code' AS discharge_code
7882
FROM encounter r;
7983

8084
-- ============================================================
@@ -120,12 +124,50 @@ SELECT
120124
parse_fhir_datetime(r.resource->'effective'->>'dateTime'),
121125
parse_fhir_datetime(r.resource->'effective'->'Period'->>'end')
122126
) AS effective_end,
123-
-- Aidbox polymorphic: value -> {CodeableConcept: {coding: [...]}}
127+
-- Aidbox polymorphic: value -> {Quantity: {value, unit}} OR {CodeableConcept: {coding: [...]}}
128+
(r.resource->'value'->'Quantity'->>'value') AS value_quantity,
129+
(r.resource->'value'->'Quantity'->>'unit') AS value_unit,
124130
r.resource->'value'->'CodeableConcept'->'coding'->0->>'system' AS value_system,
125131
r.resource->'value'->'CodeableConcept'->'coding'->0->>'code' AS value_code,
126-
CASE WHEN r.resource->'value' IS NOT NULL THEN true ELSE false END AS has_value
132+
CASE WHEN r.resource->'value' IS NOT NULL THEN true ELSE false END AS has_value,
133+
parse_fhir_datetime(r.resource->>'issued') AS issued,
134+
-- qicore-notDoneReason extension (CMS143/CMS149 use it for exception logic)
135+
(SELECT ext->'value'->'CodeableConcept'->'coding'->0->>'system'
136+
FROM jsonb_array_elements(r.resource->'extension') ext
137+
WHERE ext->>'url' = 'http://hl7.org/fhir/us/qicore/StructureDefinition/qicore-notDoneReason'
138+
LIMIT 1) AS not_done_reason_system,
139+
(SELECT ext->'value'->'CodeableConcept'->'coding'->0->>'code'
140+
FROM jsonb_array_elements(r.resource->'extension') ext
141+
WHERE ext->>'url' = 'http://hl7.org/fhir/us/qicore/StructureDefinition/qicore-notDoneReason'
142+
LIMIT 1) AS not_done_reason_code
127143
FROM observation r;
128144

145+
-- ============================================================
146+
-- Observation (BP panel, CMS165) — pre-filtered to LOINC 85354-9
147+
-- with systolic (8480-6) and diastolic (8462-4) components projected
148+
-- ============================================================
149+
DROP VIEW IF EXISTS observation_bp_flat CASCADE;
150+
CREATE VIEW observation_bp_flat AS
151+
SELECT
152+
r.id,
153+
r.resource->'subject'->>'id' AS patient_id,
154+
r.resource->>'status' AS status,
155+
COALESCE(
156+
parse_fhir_datetime(r.resource->'effective'->>'dateTime'),
157+
parse_fhir_datetime(r.resource->'effective'->'Period'->>'start')
158+
) AS effective_date,
159+
r.resource->'encounter'->>'id' AS encounter_id,
160+
(SELECT (c->'value'->'Quantity'->>'value')::numeric
161+
FROM jsonb_array_elements(r.resource->'component') c
162+
WHERE c->'code'->'coding'->0->>'code' = '8480-6'
163+
LIMIT 1) AS systolic,
164+
(SELECT (c->'value'->'Quantity'->>'value')::numeric
165+
FROM jsonb_array_elements(r.resource->'component') c
166+
WHERE c->'code'->'coding'->0->>'code' = '8462-4'
167+
LIMIT 1) AS diastolic
168+
FROM observation r
169+
WHERE r.resource->'code'->'coding'->0->>'code' = '85354-9';
170+
129171
-- ============================================================
130172
-- Condition
131173
-- ============================================================
@@ -147,7 +189,9 @@ SELECT
147189
parse_fhir_datetime(r.resource->'abatement'->>'dateTime'),
148190
parse_fhir_datetime(r.resource->'abatement'->'Period'->>'end')
149191
) AS abatement_date,
150-
r.resource->'category'->0->'coding'->0->>'code' AS category_code
192+
r.resource->'category'->0->'coding'->0->>'code' AS category_code,
193+
-- Condition.bodySite — used by CMS125 for mastectomy laterality (24028007 Right, 7771000 Left)
194+
r.resource->'bodySite'->0->'coding'->0->>'code' AS body_site_code
151195
FROM condition r;
152196

153197
-- ============================================================

0 commit comments

Comments
 (0)