Skip to content

Commit 2be32a0

Browse files
committed
update postgres concepts with latest transpilation
1 parent beeb69b commit 2be32a0

File tree

20 files changed

+126
-57
lines changed

20 files changed

+126
-57
lines changed

mimic-iv/concepts_postgres/demographics/age.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ SELECT
77
ad.admittime,
88
pa.anchor_age,
99
pa.anchor_year, /* calculate the age as anchor_age (60) plus difference between */ /* admit year and the anchor year. */ /* the noqa retains the extra long line so the */ /* convert to postgres bash script works */
10-
pa.anchor_age + EXTRACT(EPOCH FROM ad.admittime - TO_TIMESTAMP(TO_CHAR(pa.anchor_year, '0000') || TO_CHAR(1, '00') || TO_CHAR(1, '00') || TO_CHAR(0, '00') || TO_CHAR(0, '00') || TO_CHAR(0, '00'), 'yyyymmddHH24MISS')) / 31556908.8 AS age /* noqa: L016 */
10+
pa.anchor_age + EXTRACT(EPOCH FROM ad.admittime - MAKE_TIMESTAMP(pa.anchor_year, 1, 1, 0, 0, 0)) / 31556908.8 AS age /* noqa: L016 */
1111
FROM mimiciv_hosp.admissions AS ad
1212
INNER JOIN mimiciv_hosp.patients AS pa
1313
ON ad.subject_id = pa.subject_id

mimic-iv/concepts_postgres/demographics/icustay_detail.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SELECT
99
adm.admittime,
1010
adm.dischtime,
1111
EXTRACT(EPOCH FROM adm.dischtime - adm.admittime) / 86400.0 AS los_hospital, /* calculate the age as anchor_age (60) plus difference between */ /* admit year and the anchor year. */ /* the noqa retains the extra long line so the */ /* convert to postgres bash script works */
12-
pat.anchor_age + EXTRACT(EPOCH FROM adm.admittime - TO_TIMESTAMP(TO_CHAR(pat.anchor_year, '0000') || TO_CHAR(1, '00') || TO_CHAR(1, '00') || TO_CHAR(0, '00') || TO_CHAR(0, '00') || TO_CHAR(0, '00'), 'yyyymmddHH24MISS')) / 31556908.8 AS admission_age, /* noqa: L016 */
12+
pat.anchor_age + EXTRACT(EPOCH FROM adm.admittime - MAKE_TIMESTAMP(pat.anchor_year, 1, 1, 0, 0, 0)) / 31556908.8 AS admission_age, /* noqa: L016 */
1313
adm.race,
1414
adm.hospital_expire_flag,
1515
DENSE_RANK() OVER (PARTITION BY adm.subject_id ORDER BY adm.admittime NULLS FIRST) AS hospstay_seq,

mimic-iv/concepts_postgres/demographics/icustay_hourly.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ WITH all_hours AS (
99
THEN it.intime_hr
1010
ELSE DATE_TRUNC('HOUR', it.intime_hr) + INTERVAL '1 HOUR'
1111
END AS endtime, /* create integers for each charttime in hours from admission */ /* so 0 is admission time, 1 is one hour after admission, etc, */ /* up to ICU disch */ /* we allow 24 hours before ICU admission (to grab labs before admit) */
12-
GENERATE_SERIES(-24, CAST(CEIL(EXTRACT(EPOCH FROM it.outtime_hr - it.intime_hr) / 3600.0) AS INT)) AS hrs /* noqa: L016 */
12+
ARRAY(SELECT
13+
*
14+
FROM GENERATE_SERIES(-24, CAST(CEIL(EXTRACT(EPOCH FROM it.outtime_hr - it.intime_hr) / 3600.0) AS INT))) AS hrs /* noqa: L016 */
1315
FROM mimiciv_derived.icustay_times AS it
1416
)
1517
SELECT
1618
stay_id,
1719
CAST(hr_unnested AS BIGINT) AS hr,
1820
endtime + CAST(hr_unnested AS BIGINT) * INTERVAL '1 HOUR' AS endtime
1921
FROM all_hours
20-
CROSS JOIN UNNEST(all_hours.hrs) AS _t(hr_unnested)
22+
CROSS JOIN UNNEST(all_hours.hrs) AS _t0(hr_unnested)

mimic-iv/concepts_postgres/firstday/first_day_urine_output.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ FROM mimiciv_icu.icustays AS ie
99
/* Join to the outputevents table to get urine output */
1010
LEFT JOIN mimiciv_derived.urine_output AS uo
1111
ON ie.stay_id = uo.stay_id
12-
AND uo.charttime >= ie.intime
12+
AND /* ensure the data occurs during the first day */ uo.charttime >= ie.intime
1313
AND uo.charttime <= ie.intime + INTERVAL '1 DAY'
1414
GROUP BY
1515
ie.subject_id,

mimic-iv/concepts_postgres/firstday/first_day_weight.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ SELECT
1111
FROM mimiciv_icu.icustays AS ie
1212
/* admission weight */
1313
LEFT JOIN mimiciv_derived.weight_durations AS ce
14-
ON ie.stay_id = ce.stay_id AND ce.starttime <= ie.intime + INTERVAL '1 DAY'
14+
ON ie.stay_id = ce.stay_id
15+
AND /* we filter to weights documented during or before the 1st day */ ce.starttime <= ie.intime + INTERVAL '1 DAY'
1516
GROUP BY
1617
ie.subject_id,
1718
ie.stay_id

mimic-iv/concepts_postgres/measurement/bg.sql

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ DROP TABLE IF EXISTS mimiciv_derived.bg; CREATE TABLE mimiciv_derived.bg AS
33
/* The aim of this query is to pivot entries related to blood gases */ /* which were found in LABEVENTS */
44
WITH bg AS (
55
SELECT
6-
MAX(subject_id) AS subject_id,
6+
MAX(subject_id) AS subject_id, /* specimen_id only ever has 1 measurement for each itemid */ /* so, we may simply collapse rows using MAX() */
77
MAX(hadm_id) AS hadm_id,
88
MAX(charttime) AS charttime, /* specimen_id *may* have different storetimes, so this */ /* is taking the latest */
99
MAX(storetime) AS storetime,
@@ -57,7 +57,9 @@ WITH bg AS (
5757
AVG(valuenum) AS spo2
5858
FROM mimiciv_icu.chartevents
5959
WHERE
60-
itemid = 220277 /* O2 saturation pulseoxymetry */ AND valuenum > 0 AND valuenum <= 100
60+
itemid = 220277 /* O2 saturation pulseoxymetry */
61+
AND valuenum > 0
62+
AND valuenum <= 100
6163
GROUP BY
6264
subject_id,
6365
charttime
@@ -78,7 +80,9 @@ WITH bg AS (
7880
) AS fio2_chartevents
7981
FROM mimiciv_icu.chartevents
8082
WHERE
81-
itemid = 223835 /* Inspired O2 Fraction (FiO2) */ AND valuenum > 0 AND valuenum <= 100
83+
itemid = 223835 /* Inspired O2 Fraction (FiO2) */
84+
AND valuenum > 0
85+
AND valuenum <= 100
8286
GROUP BY
8387
subject_id,
8488
charttime
@@ -90,7 +94,7 @@ WITH bg AS (
9094
FROM bg
9195
LEFT JOIN stg_spo2 AS s1
9296
ON bg.subject_id = s1.subject_id
93-
AND s1.charttime BETWEEN bg.charttime - INTERVAL '2 HOUR' AND bg.charttime
97+
AND /* spo2 occurred at most 2 hours before this blood gas */ s1.charttime BETWEEN bg.charttime - INTERVAL '2 HOUR' AND bg.charttime
9498
WHERE
9599
NOT bg.po2 IS NULL
96100
), stg3 AS (
@@ -101,7 +105,7 @@ WITH bg AS (
101105
FROM stg2 AS bg
102106
LEFT JOIN stg_fio2 AS s2
103107
ON bg.subject_id = s2.subject_id
104-
AND s2.charttime >= bg.charttime - INTERVAL '4 HOUR'
108+
AND /* fio2 occurred at most 4 hours before this blood gas */ s2.charttime >= bg.charttime - INTERVAL '4 HOUR'
105109
AND s2.charttime <= bg.charttime
106110
AND s2.fio2_chartevents > 0
107111
/* only the row with the most recent SpO2 (if no SpO2 found lastRowSpO2 = 1) */

mimic-iv/concepts_postgres/measurement/blood_differential.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ WITH blood_diff AS (
5656
MAX(CASE WHEN itemid = 51257 THEN valuenum ELSE NULL END) AS nrbc, /* utility flags which determine whether imputation is possible */
5757
CASE
5858
WHEN MAX(CASE WHEN itemid IN (51300, 51301, 51755) THEN valuenum ELSE NULL END) > 0
59-
AND SUM(
59+
AND /* and we have at least one percentage from the diff */ /* sometimes the entire diff is 0%, which looks like bad data */ SUM(
6060
CASE
6161
WHEN itemid IN (51146, 51200, 51244, 51245, 51254, 51256)
6262
THEN valuenum
@@ -70,7 +70,7 @@ WITH blood_diff AS (
7070
WHERE
7171
le.itemid IN (51146 /* basophils */, 52069 /* Absolute basophil count */, 51199 /* Eosinophil Count */, 51200 /* Eosinophils */, 52073 /* Absolute Eosinophil count */, 51244 /* Lymphocytes */, 51245 /* Lymphocytes, Percent */, 51133 /* Absolute Lymphocyte Count */, 52769 /* Absolute Lymphocyte Count */, 51253 /* Monocyte Count */, 51254 /* Monocytes */, 52074 /* Absolute Monocyte Count */, 51256 /* Neutrophils */, 52075 /* Absolute Neutrophil Count */, 51143 /* Atypical lymphocytes */, 51144 /* Bands (%) */, 51218 /* Granulocyte Count */, 52135 /* Immature granulocytes (%) */, 51251 /* Metamyelocytes */, 51257 /* Nucleated Red Cells */ /* wbc totals measured in K/uL */ /* 52220 (wbcp) is percentage */, 51300, 51301, 51755) /* below are point of care tests which are extremely infrequent */ /* and usually low quality */ /* 51697, -- Neutrophils (mmol/L) */ /* below itemid do not have data as of MIMIC-IV v1.0 */ /* 51536, -- Absolute Lymphocyte Count */ /* 51537, -- Absolute Neutrophil */ /* 51690, -- Lymphocytes */ /* 52151, -- NRBC */
7272
AND NOT valuenum IS NULL
73-
AND valuenum >= 0
73+
AND /* differential values cannot be negative */ valuenum >= 0
7474
GROUP BY
7575
le.specimen_id
7676
)

mimic-iv/concepts_postgres/measurement/chemistry.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ FROM mimiciv_hosp.labevents AS le
2222
WHERE
2323
le.itemid IN (50862 /* comment is: LABEL | CATEGORY | FLUID | NUMBER OF ROWS IN LABEVENTS */ /* ALBUMIN | CHEMISTRY | BLOOD | 146697 */, 50930 /* Globulin */, 50976 /* Total protein */ /* 52456, -- Anion gap, point of care test */, 50868 /* ANION GAP | CHEMISTRY | BLOOD | 769895 */, 50882 /* BICARBONATE | CHEMISTRY | BLOOD | 780733 */, 50893 /* Calcium */ /* 52502, Creatinine, point of care */, 50912 /* CREATININE | CHEMISTRY | BLOOD | 797476 */, 50902 /* CHLORIDE | CHEMISTRY | BLOOD | 795568 */, 50931 /* GLUCOSE | CHEMISTRY | BLOOD | 748981 */ /* 52525, Glucose, point of care */ /* 52566, -- Potassium, point of care */, 50971 /* POTASSIUM | CHEMISTRY | BLOOD | 845825 */ /* 52579, -- Sodium, point of care */, 50983 /* SODIUM | CHEMISTRY | BLOOD | 808489 */ /* 52603, Urea, point of care */, 51006 /* UREA NITROGEN | CHEMISTRY | BLOOD | 791925 */)
2424
AND NOT valuenum IS NULL
25-
AND (
25+
AND /* lab values cannot be 0 and cannot be negative */ /* .. except anion gap. */ (
2626
valuenum > 0 OR itemid = 50868
27-
) /* lab values cannot be 0 and cannot be negative */ /* .. except anion gap. */
27+
)
2828
GROUP BY
2929
le.specimen_id

mimic-iv/concepts_postgres/measurement/complete_blood_count.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ FROM mimiciv_hosp.labevents AS le
2020
WHERE
2121
le.itemid IN (51221 /* hematocrit */, 51222 /* hemoglobin */, 51248 /* MCH */, 51249 /* MCHC */, 51250 /* MCV */, 51265 /* platelets */, 51279 /* RBC */, 51277 /* RDW */, 52159 /* RDW SD */, 51301 /* WBC */)
2222
AND NOT valuenum IS NULL
23-
AND valuenum > 0
23+
AND /* lab values cannot be 0 and cannot be negative */ valuenum > 0
2424
GROUP BY
2525
le.specimen_id

mimic-iv/concepts_postgres/measurement/enzyme.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ FROM mimiciv_hosp.labevents AS le
2121
WHERE
2222
le.itemid IN (50861 /* Alanine transaminase (ALT) */, 50863 /* Alkaline phosphatase (ALP) */, 50878 /* Aspartate transaminase (AST) */, 50867 /* Amylase */, 50885 /* total bili */, 50884 /* indirect bili */, 50883 /* direct bili */, 50910 /* ck_cpk */, 50911 /* CK-MB */, 50927 /* Gamma Glutamyltransferase (GGT) */, 50954 /* ld_ldh */)
2323
AND NOT valuenum IS NULL
24-
AND valuenum > 0
24+
AND /* lab values cannot be 0 and cannot be negative */ valuenum > 0
2525
GROUP BY
2626
le.specimen_id

0 commit comments

Comments
 (0)