Skip to content

Commit

Permalink
update postgres concepts with mimic-iv v2.0 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alistairewj committed Jul 11, 2022
1 parent aad8ea6 commit 43a4245
Show file tree
Hide file tree
Showing 59 changed files with 240 additions and 261 deletions.
8 changes: 4 additions & 4 deletions mimic-iv/concepts/postgres/comorbidity/charlson.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ WITH diag AS
hadm_id
, CASE WHEN icd_version = 9 THEN icd_code ELSE NULL END AS icd9_code
, CASE WHEN icd_version = 10 THEN icd_code ELSE NULL END AS icd10_code
FROM mimic_hosp.diagnoses_icd diag
FROM mimiciv_hosp.diagnoses_icd diag
)
, com AS
(
Expand Down Expand Up @@ -255,7 +255,7 @@ WITH diag AS
SUBSTR(icd10_code, 1, 3) IN ('B20','B21','B22','B24')
THEN 1
ELSE 0 END) AS aids
FROM mimic_core.admissions ad
FROM mimiciv_hosp.admissions ad
LEFT JOIN diag
ON ad.hadm_id = diag.hadm_id
GROUP BY ad.hadm_id
Expand All @@ -270,7 +270,7 @@ WITH diag AS
WHEN age <= 60 THEN 2
WHEN age <= 70 THEN 3
ELSE 4 END AS age_score
FROM mimic_derived.age
FROM mimiciv_derived.age
)
SELECT
ad.subject_id
Expand Down Expand Up @@ -305,7 +305,7 @@ SELECT
+ 2*paraplegia + 2*renal_disease
+ 6*aids
AS charlson_comorbidity_index
FROM mimic_core.admissions ad
FROM mimiciv_hosp.admissions ad
LEFT JOIN com
ON ad.hadm_id = com.hadm_id
LEFT JOIN ag
Expand Down
6 changes: 3 additions & 3 deletions mimic-iv/concepts/postgres/demographics/age.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ SELECT
, ad.admittime
, pa.anchor_age
, pa.anchor_year
, DATETIME_DIFF(ad.admittime, DATETIME(pa.anchor_year, 1, 1, 0, 0,0),'YEAR') + pa.anchor_age AS age
FROM mimic_core.admissions ad
INNER JOIN mimic_core.patients pa
, DATETIME_DIFF(ad.admittime, DATETIME(pa.anchor_year, 1, 1, 0, 0, 0), YEAR) + pa.anchor_age AS age
FROM mimiciv_hosp.admissions ad
INNER JOIN mimiciv_hosp.patients pa
ON ad.subject_id = pa.subject_id
;
14 changes: 7 additions & 7 deletions mimic-iv/concepts/postgres/demographics/icustay_detail.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ SELECT ie.subject_id, ie.hadm_id, ie.stay_id

-- hospital level factors
, adm.admittime, adm.dischtime
, DATETIME_DIFF(adm.dischtime,adm.admittime,'DAY') as los_hospital
, DATETIME_DIFF(adm.admittime, DATETIME(pat.anchor_year, 1, 1, 0, 0,0),'YEAR') + pat.anchor_age as admission_age
, adm.ethnicity
, DATETIME_DIFF(adm.dischtime, adm.admittime, 'DAY') as los_hospital
, DATETIME_DIFF(adm.admittime, DATETIME(pat.anchor_year, 1, 1, 0, 0, 0), YEAR) + pat.anchor_age as admission_age
, adm.race
, adm.hospital_expire_flag
, DENSE_RANK() OVER (PARTITION BY adm.subject_id ORDER BY adm.admittime) AS hospstay_seq
, CASE
Expand All @@ -18,16 +18,16 @@ SELECT ie.subject_id, ie.hadm_id, ie.stay_id

-- icu level factors
, ie.intime as icu_intime, ie.outtime as icu_outtime
, ROUND( CAST( DATETIME_DIFF(ie.outtime,ie.intime,'HOUR')/24.0 as numeric),2) as los_icu
, ROUND( CAST( DATETIME_DIFF(ie.outtime as numeric),ie.intime, 'HOUR')/24.0, 2) as los_icu
, DENSE_RANK() OVER (PARTITION BY ie.hadm_id ORDER BY ie.intime) AS icustay_seq

-- first ICU stay *for the current hospitalization*
, CASE
WHEN DENSE_RANK() OVER (PARTITION BY ie.hadm_id ORDER BY ie.intime) = 1 THEN True
ELSE False END AS first_icu_stay

FROM mimic_icu.icustays ie
INNER JOIN mimic_core.admissions adm
FROM mimiciv_icu.icustays ie
INNER JOIN mimiciv_hosp.admissions adm
ON ie.hadm_id = adm.hadm_id
INNER JOIN mimic_core.patients pat
INNER JOIN mimiciv_hosp.patients pat
ON ie.subject_id = pat.subject_id
4 changes: 2 additions & 2 deletions mimic-iv/concepts/postgres/demographics/icustay_hourly.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ select
-- 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)
, ARRAY(SELECT * FROM generate_series(-24, CEIL(DATETIME_DIFF(it.outtime_hr,it.intime_hr,'HOUR')))) as hrs
, ARRAY(SELECT * FROM generate_series(-24, CEIL(DATETIME_DIFF(it.outtime_hr, it.intime_hr, 'HOUR')))) as hrs

from mimic_derived.icustay_times it
from mimiciv_derived.icustay_times it
)
SELECT stay_id
, CAST(hr AS bigint) as hr
Expand Down
4 changes: 2 additions & 2 deletions mimic-iv/concepts/postgres/demographics/icustay_times.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ WITH t1 AS
select ce.stay_id
, min(charttime) as intime_hr
, max(charttime) as outtime_hr
FROM mimic_icu.chartevents ce
FROM mimiciv_icu.chartevents ce
-- only look at heart rate
where ce.itemid = 220045
group by ce.stay_id
Expand All @@ -18,6 +18,6 @@ select
ie.subject_id, ie.hadm_id, ie.stay_id
, t1.intime_hr
, t1.outtime_hr
FROM mimic_icu.icustays ie
FROM mimiciv_icu.icustays ie
left join t1
on ie.stay_id = t1.stay_id;
6 changes: 3 additions & 3 deletions mimic-iv/concepts/postgres/demographics/weight_durations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ WITH wt_stg as
else 'daily' end as weight_type
-- TODO: eliminate obvious outliers if there is a reasonable weight
, c.valuenum as weight
FROM mimic_icu.chartevents c
FROM mimiciv_icu.chartevents c
WHERE c.valuenum IS NOT NULL
AND c.itemid in
(
Expand Down Expand Up @@ -44,7 +44,7 @@ WITH wt_stg as
else wt_stg1.charttime end as starttime
, wt_stg1.weight
from wt_stg1
INNER JOIN mimic_icu.icustays ie
INNER JOIN mimiciv_icu.icustays ie
on ie.stay_id = wt_stg1.stay_id
)
, wt_stg3 as
Expand Down Expand Up @@ -89,7 +89,7 @@ WITH wt_stg as
, wt.starttime as endtime
, wt.weight
, wt.weight_type
from mimic_icu.icustays ie
from mimiciv_icu.icustays ie
inner join
-- the below subquery returns one row for each unique stay_id
-- the row contains: the first starttime and the corresponding weight
Expand Down
4 changes: 2 additions & 2 deletions mimic-iv/concepts/postgres/firstday/first_day_bg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ select
, MIN(glucose) AS glucose_min, MAX(glucose) AS glucose_max
, MIN(potassium) AS potassium_min, MAX(potassium) AS potassium_max
, MIN(sodium) AS sodium_min, MAX(sodium) AS sodium_max
FROM mimic_icu.icustays ie
LEFT JOIN mimic_derived.bg bg
FROM mimiciv_icu.icustays ie
LEFT JOIN mimiciv_derived.bg bg
ON ie.subject_id = bg.subject_id
AND bg.charttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND bg.charttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
Expand Down
4 changes: 2 additions & 2 deletions mimic-iv/concepts/postgres/firstday/first_day_bg_art.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ select
, MIN(glucose) AS glucose_min, MAX(glucose) AS glucose_max
, MIN(potassium) AS potassium_min, MAX(potassium) AS potassium_max
, MIN(sodium) AS sodium_min, MAX(sodium) AS sodium_max
FROM mimic_icu.icustays ie
LEFT JOIN mimic_derived.bg bg
FROM mimiciv_icu.icustays ie
LEFT JOIN mimiciv_derived.bg bg
ON ie.subject_id = bg.subject_id
AND bg.specimen = 'ART.'
AND bg.charttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
Expand Down
4 changes: 2 additions & 2 deletions mimic-iv/concepts/postgres/firstday/first_day_gcs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ WITH gcs_final AS
PARTITION BY gcs.stay_id
ORDER BY gcs.GCS
) as gcs_seq
FROM mimic_derived.gcs gcs
FROM mimiciv_derived.gcs gcs
)
SELECT
ie.subject_id
Expand All @@ -35,7 +35,7 @@ SELECT
, gcs_verbal
, gcs_eyes
, gcs_unable
FROM mimic_icu.icustays ie
FROM mimiciv_icu.icustays ie
LEFT JOIN gcs_final gs
ON ie.stay_id = gs.stay_id
AND gs.gcs_seq = 1
Expand Down
8 changes: 4 additions & 4 deletions mimic-iv/concepts/postgres/firstday/first_day_height.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ WITH ce AS
SELECT
c.stay_id
, AVG(valuenum) as Height_chart
FROM mimic_icu.chartevents c
INNER JOIN mimic_icu.icustays ie ON
FROM mimiciv_icu.chartevents c
INNER JOIN mimiciv_icu.icustays ie ON
c.stay_id = ie.stay_id
AND c.charttime BETWEEN DATETIME_SUB(ie.intime, INTERVAL '1' DAY) AND DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
WHERE c.valuenum IS NOT NULL
Expand All @@ -24,8 +24,8 @@ SELECT
ie.subject_id
, ie.stay_id
, ROUND( CAST( AVG(height) as numeric),2) AS height
FROM mimic_icu.icustays ie
LEFT JOIN mimic_derived.height ht
FROM mimiciv_icu.icustays ie
LEFT JOIN mimiciv_derived.height ht
ON ie.stay_id = ht.stay_id
AND ht.charttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND ht.charttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
Expand Down
22 changes: 11 additions & 11 deletions mimic-iv/concepts/postgres/firstday/first_day_lab.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ WITH cbc AS
, MAX(platelet) as platelets_max
, MIN(wbc) as wbc_min
, MAX(wbc) as wbc_max
FROM mimic_icu.icustays ie
LEFT JOIN mimic_derived.complete_blood_count le
FROM mimiciv_icu.icustays ie
LEFT JOIN mimiciv_derived.complete_blood_count le
ON le.subject_id = ie.subject_id
AND le.charttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND le.charttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
Expand All @@ -35,8 +35,8 @@ WITH cbc AS
, MIN(glucose) AS glucose_min, MAX(glucose) AS glucose_max
, MIN(sodium) AS sodium_min, MAX(sodium) AS sodium_max
, MIN(potassium) AS potassium_min, MAX(potassium) AS potassium_max
FROM mimic_icu.icustays ie
LEFT JOIN mimic_derived.chemistry le
FROM mimiciv_icu.icustays ie
LEFT JOIN mimiciv_derived.chemistry le
ON le.subject_id = ie.subject_id
AND le.charttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND le.charttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
Expand All @@ -56,8 +56,8 @@ WITH cbc AS
, MIN(immature_granulocytes) AS imm_granulocytes_min, MAX(immature_granulocytes) AS imm_granulocytes_max
, MIN(metamyelocytes) AS metas_min, MAX(metamyelocytes) AS metas_max
, MIN(nrbc) AS nrbc_min, MAX(nrbc) AS nrbc_max
FROM mimic_icu.icustays ie
LEFT JOIN mimic_derived.blood_differential le
FROM mimiciv_icu.icustays ie
LEFT JOIN mimiciv_derived.blood_differential le
ON le.subject_id = ie.subject_id
AND le.charttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND le.charttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
Expand All @@ -73,8 +73,8 @@ WITH cbc AS
, MIN(inr) AS inr_min, MAX(inr) AS inr_max
, MIN(pt) AS pt_min, MAX(pt) AS pt_max
, MIN(ptt) AS ptt_min, MAX(ptt) AS ptt_max
FROM mimic_icu.icustays ie
LEFT JOIN mimic_derived.coagulation le
FROM mimiciv_icu.icustays ie
LEFT JOIN mimiciv_derived.coagulation le
ON le.subject_id = ie.subject_id
AND le.charttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND le.charttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
Expand All @@ -96,8 +96,8 @@ WITH cbc AS
, MIN(ck_mb) AS ck_mb_min, MAX(ck_mb) AS ck_mb_max
, MIN(ggt) AS ggt_min, MAX(ggt) AS ggt_max
, MIN(ld_ldh) AS ld_ldh_min, MAX(ld_ldh) AS ld_ldh_max
FROM mimic_icu.icustays ie
LEFT JOIN mimic_derived.enzyme le
FROM mimiciv_icu.icustays ie
LEFT JOIN mimiciv_derived.enzyme le
ON le.subject_id = ie.subject_id
AND le.charttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND le.charttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
Expand Down Expand Up @@ -154,7 +154,7 @@ ie.subject_id
, ck_mb_min, ck_mb_max
, ggt_min, ggt_max
, ld_ldh_min, ld_ldh_max
FROM mimic_icu.icustays ie
FROM mimiciv_icu.icustays ie
LEFT JOIN cbc
ON ie.stay_id = cbc.stay_id
LEFT JOIN chem
Expand Down
4 changes: 2 additions & 2 deletions mimic-iv/concepts/postgres/firstday/first_day_rrt.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ select
, MAX(dialysis_present) AS dialysis_present
, MAX(dialysis_active) AS dialysis_active
, STRING_AGG(DISTINCT dialysis_type, ', ') AS dialysis_type
FROM mimic_icu.icustays ie
LEFT JOIN mimic_derived.rrt rrt
FROM mimiciv_icu.icustays ie
LEFT JOIN mimiciv_derived.rrt rrt
ON ie.stay_id = rrt.stay_id
AND rrt.charttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND rrt.charttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
Expand Down
36 changes: 18 additions & 18 deletions mimic-iv/concepts/postgres/firstday/first_day_sofa.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,29 @@ DROP TABLE IF EXISTS first_day_sofa; CREATE TABLE first_day_sofa AS
with vaso_stg as
(
select ie.stay_id, 'norepinephrine' AS treatment, vaso_rate as rate
FROM mimic_icu.icustays ie
INNER JOIN mimic_derived.norepinephrine mv
FROM mimiciv_icu.icustays ie
INNER JOIN mimiciv_derived.norepinephrine mv
ON ie.stay_id = mv.stay_id
AND mv.starttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND mv.starttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
UNION ALL
select ie.stay_id, 'epinephrine' AS treatment, vaso_rate as rate
FROM mimic_icu.icustays ie
INNER JOIN mimic_derived.epinephrine mv
FROM mimiciv_icu.icustays ie
INNER JOIN mimiciv_derived.epinephrine mv
ON ie.stay_id = mv.stay_id
AND mv.starttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND mv.starttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
UNION ALL
select ie.stay_id, 'dobutamine' AS treatment, vaso_rate as rate
FROM mimic_icu.icustays ie
INNER JOIN mimic_derived.dobutamine mv
FROM mimiciv_icu.icustays ie
INNER JOIN mimiciv_derived.dobutamine mv
ON ie.stay_id = mv.stay_id
AND mv.starttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND mv.starttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
UNION ALL
select ie.stay_id, 'dopamine' AS treatment, vaso_rate as rate
FROM mimic_icu.icustays ie
INNER JOIN mimic_derived.dopamine mv
FROM mimiciv_icu.icustays ie
INNER JOIN mimiciv_derived.dopamine mv
ON ie.stay_id = mv.stay_id
AND mv.starttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND mv.starttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
Expand All @@ -66,7 +66,7 @@ with vaso_stg as
, max(CASE WHEN treatment = 'epinephrine' THEN rate ELSE NULL END) as rate_epinephrine
, max(CASE WHEN treatment = 'dopamine' THEN rate ELSE NULL END) as rate_dopamine
, max(CASE WHEN treatment = 'dobutamine' THEN rate ELSE NULL END) as rate_dobutamine
from mimic_icu.icustays ie
from mimiciv_icu.icustays ie
LEFT JOIN vaso_stg v
ON ie.stay_id = v.stay_id
GROUP BY ie.stay_id
Expand All @@ -77,12 +77,12 @@ with vaso_stg as
select ie.stay_id, bg.charttime
, bg.pao2fio2ratio
, case when vd.stay_id is not null then 1 else 0 end as IsVent
from mimic_icu.icustays ie
LEFT JOIN mimic_derived.bg bg
from mimiciv_icu.icustays ie
LEFT JOIN mimiciv_derived.bg bg
ON ie.subject_id = bg.subject_id
AND bg.charttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND bg.charttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
LEFT JOIN mimic_derived.ventilation vd
LEFT JOIN mimiciv_derived.ventilation vd
ON ie.stay_id = vd.stay_id
AND bg.charttime >= vd.starttime
AND bg.charttime <= vd.endtime
Expand Down Expand Up @@ -119,18 +119,18 @@ select ie.stay_id
, uo.UrineOutput

, gcs.gcs_min
from mimic_icu.icustays ie
from mimiciv_icu.icustays ie
left join vaso_mv mv
on ie.stay_id = mv.stay_id
left join pafi2 pf
on ie.stay_id = pf.stay_id
left join mimic_derived.first_day_vitalsign v
left join mimiciv_derived.first_day_vitalsign v
on ie.stay_id = v.stay_id
left join mimic_derived.first_day_lab l
left join mimiciv_derived.first_day_lab l
on ie.stay_id = l.stay_id
left join mimic_derived.first_day_urine_output uo
left join mimiciv_derived.first_day_urine_output uo
on ie.stay_id = uo.stay_id
left join mimic_derived.first_day_gcs gcs
left join mimiciv_derived.first_day_gcs gcs
on ie.stay_id = gcs.stay_id
)
, scorecalc as
Expand Down Expand Up @@ -219,7 +219,7 @@ select ie.subject_id, ie.hadm_id, ie.stay_id
, cardiovascular
, cns
, renal
from mimic_icu.icustays ie
from mimiciv_icu.icustays ie
left join scorecalc s
on ie.stay_id = s.stay_id
;
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ SELECT
ie.subject_id
, ie.stay_id
, SUM(urineoutput) AS urineoutput
FROM mimic_icu.icustays ie
FROM mimiciv_icu.icustays ie
-- Join to the outputevents table to get urine output
LEFT JOIN mimic_derived.urine_output uo
LEFT JOIN mimiciv_derived.urine_output uo
ON ie.stay_id = uo.stay_id
-- ensure the data occurs during the first day
AND uo.charttime >= ie.intime
Expand Down
4 changes: 2 additions & 2 deletions mimic-iv/concepts/postgres/firstday/first_day_vitalsign.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ ie.subject_id
, MIN(glucose) AS glucose_min
, MAX(glucose) AS glucose_max
, AVG(glucose) AS glucose_mean
FROM mimic_icu.icustays ie
LEFT JOIN mimic_derived.vitalsign ce
FROM mimiciv_icu.icustays ie
LEFT JOIN mimiciv_derived.vitalsign ce
ON ie.stay_id = ce.stay_id
AND ce.charttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND ce.charttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
Expand Down
Loading

0 comments on commit 43a4245

Please sign in to comment.