Skip to content

Commit 7861433

Browse files
committed
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
1 parent 4cf9855 commit 7861433

3 files changed

Lines changed: 56 additions & 13 deletions

File tree

mimic-iv/concepts/measurement/oxygen_delivery.sql

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ WITH ce_stg1 AS (
4747
FROM ce_stg1 ce
4848
)
4949

50+
-- filter rn before joining so device-only rows from the FULL OUTER JOIN
51+
-- are retained (a post-join WHERE ce.rn = 1 would drop rows where ce is NULL)
52+
, ce_stg3 AS (
53+
SELECT
54+
subject_id
55+
, stay_id
56+
, charttime
57+
, itemid
58+
, value
59+
, valuenum
60+
, valueuom
61+
FROM ce_stg2
62+
WHERE rn = 1
63+
)
64+
5065
, o2 AS (
5166
-- The below ITEMID can have multiple entries for charttime/storetime
5267
-- These are valid entries, and should be retained in derived tables.
@@ -85,12 +100,10 @@ WITH ce_stg1 AS (
85100
, ce.valuenum
86101
, o2.o2_device
87102
, o2.rn
88-
FROM ce_stg2 ce
103+
FROM ce_stg3 ce
89104
FULL OUTER JOIN o2
90-
ON ce.subject_id = o2.subject_id
91-
AND ce.charttime = o2.charttime
92-
-- limit to 1 row per subject_id/charttime/itemid from ce_stg2
93-
WHERE ce.rn = 1
105+
ON ce.subject_id = o2.subject_id
106+
AND ce.charttime = o2.charttime
94107
)
95108

96109
SELECT

mimic-iv/concepts_duckdb/measurement/oxygen_delivery.sql

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ WITH ce_stg1 AS (
2424
valueuom,
2525
ROW_NUMBER() OVER (PARTITION BY subject_id, charttime, itemid ORDER BY storetime DESC, valuenum DESC) AS rn
2626
FROM ce_stg1 AS ce
27+
), ce_stg3 AS (
28+
SELECT
29+
subject_id,
30+
stay_id,
31+
charttime,
32+
itemid,
33+
value,
34+
valuenum,
35+
valueuom
36+
FROM ce_stg2
37+
WHERE
38+
rn = 1
2739
), o2 AS (
2840
SELECT
2941
subject_id,
@@ -45,11 +57,9 @@ WITH ce_stg1 AS (
4557
ce.valuenum,
4658
o2.o2_device,
4759
o2.rn
48-
FROM ce_stg2 AS ce
60+
FROM ce_stg3 AS ce
4961
FULL OUTER JOIN o2
5062
ON ce.subject_id = o2.subject_id AND ce.charttime = o2.charttime
51-
WHERE
52-
ce.rn = 1
5363
)
5464
SELECT
5565
subject_id,

mimic-iv/concepts_postgres/measurement/oxygen_delivery.sql

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,31 @@ WITH ce_stg1 AS (
3232
ORDER BY storetime DESC NULLS LAST, valuenum DESC NULLS LAST
3333
) AS rn
3434
FROM ce_stg1 AS ce
35+
), 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 (
36+
SELECT
37+
subject_id,
38+
stay_id,
39+
charttime,
40+
itemid,
41+
value,
42+
valuenum,
43+
valueuom
44+
FROM ce_stg2
45+
WHERE
46+
rn = 1
3547
), o2 AS (
36-
/* 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 */
48+
/* The below ITEMID can have multiple entries for charttime/storetime */
49+
/* These are valid entries, and should be retained in derived tables. */
50+
/* 224181 -- Small Volume Neb Drug #1 | Respiratory | Text */
51+
/* , 227570 -- Small Volume Neb Drug/Dose #1 | Respiratory | Text */
52+
/* , 224833 -- SBT Deferred | Respiratory | Text */
53+
/* , 224716 -- SBT Stopped | Respiratory | Text */
54+
/* , 224740 -- RSBI Deferred | Respiratory | Text */
55+
/* , 224829 -- Trach Tube Type | Respiratory | Text */
56+
/* , 226732 -- O2 Delivery Device(s) | Respiratory | Text */
57+
/* , 226873 -- Inspiratory Ratio | Respiratory | Numeric */
58+
/* , 226871 -- Expiratory Ratio | Respiratory | Numeric */
59+
/* maximum of 4 o2 devices on at once */
3760
SELECT
3861
subject_id,
3962
stay_id,
@@ -57,12 +80,9 @@ WITH ce_stg1 AS (
5780
ce.valuenum,
5881
o2.o2_device,
5982
o2.rn
60-
FROM ce_stg2 AS ce
83+
FROM ce_stg3 AS ce
6184
FULL OUTER JOIN o2
6285
ON ce.subject_id = o2.subject_id AND ce.charttime = o2.charttime
63-
/* limit to 1 row per subject_id/charttime/itemid from ce_stg2 */
64-
WHERE
65-
ce.rn = 1
6686
)
6787
SELECT
6888
subject_id,

0 commit comments

Comments
 (0)