From 78614333f107fe0e78d603507eb9ba826028659a Mon Sep 17 00:00:00 2001 From: ani Date: Sun, 19 Jul 2026 00:21:45 -0500 Subject: [PATCH] fix(mimic-iv): retain device-only rows in oxygen_delivery The FULL OUTER JOIN of O2 flow and delivery devices filtered with WHERE ce.rn = 1, which dropped rows where only a device (itemid 226732) was charted. Filter rn in ce_stg3 before the join so device-only rows are retained. Regenerate postgres and duckdb dialect copies. Fixes #1570 --- .../concepts/measurement/oxygen_delivery.sql | 23 ++++++++++---- .../measurement/oxygen_delivery.sql | 16 ++++++++-- .../measurement/oxygen_delivery.sql | 30 +++++++++++++++---- 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/mimic-iv/concepts/measurement/oxygen_delivery.sql b/mimic-iv/concepts/measurement/oxygen_delivery.sql index e76f6303..c7a56f75 100644 --- a/mimic-iv/concepts/measurement/oxygen_delivery.sql +++ b/mimic-iv/concepts/measurement/oxygen_delivery.sql @@ -47,6 +47,21 @@ WITH ce_stg1 AS ( FROM ce_stg1 ce ) +-- filter rn before joining so device-only rows from the FULL OUTER JOIN +-- are retained (a post-join WHERE ce.rn = 1 would drop rows where ce is NULL) +, ce_stg3 AS ( + SELECT + subject_id + , stay_id + , charttime + , itemid + , value + , valuenum + , valueuom + FROM ce_stg2 + WHERE rn = 1 +) + , o2 AS ( -- The below ITEMID can have multiple entries for charttime/storetime -- These are valid entries, and should be retained in derived tables. @@ -85,12 +100,10 @@ WITH ce_stg1 AS ( , ce.valuenum , o2.o2_device , o2.rn - FROM ce_stg2 ce + FROM ce_stg3 ce FULL OUTER JOIN o2 - ON ce.subject_id = o2.subject_id - AND ce.charttime = o2.charttime - -- limit to 1 row per subject_id/charttime/itemid from ce_stg2 - WHERE ce.rn = 1 + ON ce.subject_id = o2.subject_id + AND ce.charttime = o2.charttime ) SELECT diff --git a/mimic-iv/concepts_duckdb/measurement/oxygen_delivery.sql b/mimic-iv/concepts_duckdb/measurement/oxygen_delivery.sql index 0bfef488..2289c381 100644 --- a/mimic-iv/concepts_duckdb/measurement/oxygen_delivery.sql +++ b/mimic-iv/concepts_duckdb/measurement/oxygen_delivery.sql @@ -24,6 +24,18 @@ WITH ce_stg1 AS ( valueuom, ROW_NUMBER() OVER (PARTITION BY subject_id, charttime, itemid ORDER BY storetime DESC, valuenum DESC) AS rn FROM ce_stg1 AS ce +), ce_stg3 AS ( + SELECT + subject_id, + stay_id, + charttime, + itemid, + value, + valuenum, + valueuom + FROM ce_stg2 + WHERE + rn = 1 ), o2 AS ( SELECT subject_id, @@ -45,11 +57,9 @@ WITH ce_stg1 AS ( ce.valuenum, o2.o2_device, o2.rn - FROM ce_stg2 AS ce + FROM ce_stg3 AS ce FULL OUTER JOIN o2 ON ce.subject_id = o2.subject_id AND ce.charttime = o2.charttime - WHERE - ce.rn = 1 ) SELECT subject_id, diff --git a/mimic-iv/concepts_postgres/measurement/oxygen_delivery.sql b/mimic-iv/concepts_postgres/measurement/oxygen_delivery.sql index d3594237..233f4894 100644 --- a/mimic-iv/concepts_postgres/measurement/oxygen_delivery.sql +++ b/mimic-iv/concepts_postgres/measurement/oxygen_delivery.sql @@ -32,8 +32,31 @@ WITH ce_stg1 AS ( ORDER BY storetime DESC NULLS LAST, valuenum DESC NULLS LAST ) AS rn FROM ce_stg1 AS ce +), ce_stg3 /* filter rn before joining so device-only rows from the FULL OUTER JOIN */ /* are retained (a post-join WHERE ce.rn = 1 would drop rows where ce is NULL) */ AS ( + SELECT + subject_id, + stay_id, + charttime, + itemid, + value, + valuenum, + valueuom + FROM ce_stg2 + WHERE + rn = 1 ), o2 AS ( - /* The below ITEMID can have multiple entries for charttime/storetime */ /* These are valid entries, and should be retained in derived tables. */ /* 224181 -- Small Volume Neb Drug #1 | Respiratory | Text */ /* , 227570 -- Small Volume Neb Drug/Dose #1 | Respiratory | Text */ /* , 224833 -- SBT Deferred | Respiratory | Text */ /* , 224716 -- SBT Stopped | Respiratory | Text */ /* , 224740 -- RSBI Deferred | Respiratory | Text */ /* , 224829 -- Trach Tube Type | Respiratory | Text */ /* , 226732 -- O2 Delivery Device(s) | Respiratory | Text */ /* , 226873 -- Inspiratory Ratio | Respiratory | Numeric */ /* , 226871 -- Expiratory Ratio | Respiratory | Numeric */ /* maximum of 4 o2 devices on at once */ + /* The below ITEMID can have multiple entries for charttime/storetime */ + /* These are valid entries, and should be retained in derived tables. */ + /* 224181 -- Small Volume Neb Drug #1 | Respiratory | Text */ + /* , 227570 -- Small Volume Neb Drug/Dose #1 | Respiratory | Text */ + /* , 224833 -- SBT Deferred | Respiratory | Text */ + /* , 224716 -- SBT Stopped | Respiratory | Text */ + /* , 224740 -- RSBI Deferred | Respiratory | Text */ + /* , 224829 -- Trach Tube Type | Respiratory | Text */ + /* , 226732 -- O2 Delivery Device(s) | Respiratory | Text */ + /* , 226873 -- Inspiratory Ratio | Respiratory | Numeric */ + /* , 226871 -- Expiratory Ratio | Respiratory | Numeric */ + /* maximum of 4 o2 devices on at once */ SELECT subject_id, stay_id, @@ -57,12 +80,9 @@ WITH ce_stg1 AS ( ce.valuenum, o2.o2_device, o2.rn - FROM ce_stg2 AS ce + FROM ce_stg3 AS ce FULL OUTER JOIN o2 ON ce.subject_id = o2.subject_id AND ce.charttime = o2.charttime - /* limit to 1 row per subject_id/charttime/itemid from ce_stg2 */ - WHERE - ce.rn = 1 ) SELECT subject_id,