Skip to content

Commit

Permalink
update duckdb concepts with latest transpilation
Browse files Browse the repository at this point in the history
  • Loading branch information
alistairewj committed Aug 21, 2024
1 parent b663f8e commit beeb69b
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 30 deletions.
2 changes: 1 addition & 1 deletion mimic-iv/concepts_duckdb/demographics/icustay_hourly.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ SELECT
TRY_CAST(hr_unnested AS BIGINT) AS hr,
endtime + TRY_CAST(hr_unnested AS BIGINT) * INTERVAL '1' HOUR AS endtime
FROM all_hours
CROSS JOIN UNNEST(all_hours.hrs) AS _t(hr_unnested)
CROSS JOIN UNNEST(all_hours.hrs) AS _t0(hr_unnested)
4 changes: 3 additions & 1 deletion mimic-iv/concepts_duckdb/medication/antibiotic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ FROM mimiciv_hosp.prescriptions AS pr
INNER JOIN abx
ON pr.drug = abx.drug AND pr.route = abx.route
LEFT JOIN mimiciv_icu.icustays AS ie
ON pr.hadm_id = ie.hadm_id AND pr.starttime >= ie.intime AND pr.starttime < ie.outtime
ON pr.hadm_id = ie.hadm_id
AND pr.starttime >= ie.intime
AND pr.starttime < ie.outtime
WHERE
abx.antibiotic = 1
28 changes: 21 additions & 7 deletions mimic-iv/concepts_duckdb/medication/vasoactive_agent.sql
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,32 @@ SELECT
mil.vaso_rate AS milrinone
FROM tm_lag AS t
LEFT JOIN mimiciv_derived.dobutamine AS dob
ON t.stay_id = dob.stay_id AND t.starttime >= dob.starttime AND t.endtime <= dob.endtime
ON t.stay_id = dob.stay_id
AND t.starttime >= dob.starttime
AND t.endtime <= dob.endtime
LEFT JOIN mimiciv_derived.dopamine AS dop
ON t.stay_id = dop.stay_id AND t.starttime >= dop.starttime AND t.endtime <= dop.endtime
ON t.stay_id = dop.stay_id
AND t.starttime >= dop.starttime
AND t.endtime <= dop.endtime
LEFT JOIN mimiciv_derived.epinephrine AS epi
ON t.stay_id = epi.stay_id AND t.starttime >= epi.starttime AND t.endtime <= epi.endtime
ON t.stay_id = epi.stay_id
AND t.starttime >= epi.starttime
AND t.endtime <= epi.endtime
LEFT JOIN mimiciv_derived.norepinephrine AS nor
ON t.stay_id = nor.stay_id AND t.starttime >= nor.starttime AND t.endtime <= nor.endtime
ON t.stay_id = nor.stay_id
AND t.starttime >= nor.starttime
AND t.endtime <= nor.endtime
LEFT JOIN mimiciv_derived.phenylephrine AS phe
ON t.stay_id = phe.stay_id AND t.starttime >= phe.starttime AND t.endtime <= phe.endtime
ON t.stay_id = phe.stay_id
AND t.starttime >= phe.starttime
AND t.endtime <= phe.endtime
LEFT JOIN mimiciv_derived.vasopressin AS vas
ON t.stay_id = vas.stay_id AND t.starttime >= vas.starttime AND t.endtime <= vas.endtime
ON t.stay_id = vas.stay_id
AND t.starttime >= vas.starttime
AND t.endtime <= vas.endtime
LEFT JOIN mimiciv_derived.milrinone AS mil
ON t.stay_id = mil.stay_id AND t.starttime >= mil.starttime AND t.endtime <= mil.endtime
ON t.stay_id = mil.stay_id
AND t.starttime >= mil.starttime
AND t.endtime <= mil.endtime
WHERE
NOT t.endtime IS NULL
4 changes: 3 additions & 1 deletion mimic-iv/concepts_duckdb/organfailure/kdigo_uo.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ SELECT
uo_tm_24hr
FROM uo_stg2 AS ur
LEFT JOIN mimiciv_derived.weight_durations AS wd
ON ur.stay_id = wd.stay_id AND ur.charttime >= wd.starttime AND ur.charttime < wd.endtime
ON ur.stay_id = wd.stay_id
AND ur.charttime >= wd.starttime
AND ur.charttime < wd.endtime
15 changes: 11 additions & 4 deletions mimic-iv/concepts_duckdb/score/apsiii.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ WITH pa AS (
ROW_NUMBER() OVER (PARTITION BY ie.stay_id ORDER BY bg.po2 DESC) AS rn
FROM mimiciv_derived.bg AS bg
INNER JOIN mimiciv_icu.icustays AS ie
ON bg.hadm_id = ie.hadm_id AND bg.charttime >= ie.intime AND bg.charttime < ie.outtime
ON bg.hadm_id = ie.hadm_id
AND bg.charttime >= ie.intime
AND bg.charttime < ie.outtime
LEFT JOIN mimiciv_derived.ventilation AS vd
ON ie.stay_id = vd.stay_id
AND bg.charttime >= vd.starttime
Expand All @@ -27,7 +29,9 @@ WITH pa AS (
ROW_NUMBER() OVER (PARTITION BY ie.stay_id ORDER BY bg.aado2 DESC) AS rn
FROM mimiciv_derived.bg AS bg
INNER JOIN mimiciv_icu.icustays AS ie
ON bg.hadm_id = ie.hadm_id AND bg.charttime >= ie.intime AND bg.charttime < ie.outtime
ON bg.hadm_id = ie.hadm_id
AND bg.charttime >= ie.intime
AND bg.charttime < ie.outtime
INNER JOIN mimiciv_derived.ventilation AS vd
ON ie.stay_id = vd.stay_id
AND bg.charttime >= vd.starttime
Expand Down Expand Up @@ -62,7 +66,9 @@ WITH pa AS (
END AS acidbase_score
FROM mimiciv_derived.bg AS bg
INNER JOIN mimiciv_icu.icustays AS ie
ON bg.hadm_id = ie.hadm_id AND bg.charttime >= ie.intime AND bg.charttime < ie.outtime
ON bg.hadm_id = ie.hadm_id
AND bg.charttime >= ie.intime
AND bg.charttime < ie.outtime
WHERE
NOT ph IS NULL AND NOT pco2 IS NULL AND bg.specimen = 'ART.'
), acidbase_max AS (
Expand Down Expand Up @@ -608,7 +614,8 @@ WITH pa AS (
WHEN ABS(heart_rate_max - 75) = ABS(heart_rate_min - 75)
AND smax.hr_score >= smin.hr_score
THEN smax.hr_score
WHEN ABS(heart_rate_max - 75) = ABS(heart_rate_min - 75) AND smax.hr_score < smin.hr_score
WHEN ABS(heart_rate_max - 75) = ABS(heart_rate_min - 75)
AND smax.hr_score < smin.hr_score
THEN smin.hr_score
END AS hr_score,
CASE
Expand Down
8 changes: 6 additions & 2 deletions mimic-iv/concepts_duckdb/score/lods.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ WITH cpap AS (
CASE WHEN NOT cp.stay_id IS NULL THEN 1 ELSE 0 END AS cpap
FROM mimiciv_derived.bg AS bg
INNER JOIN mimiciv_icu.icustays AS ie
ON bg.hadm_id = ie.hadm_id AND bg.charttime >= ie.intime AND bg.charttime < ie.outtime
ON bg.hadm_id = ie.hadm_id
AND bg.charttime >= ie.intime
AND bg.charttime < ie.outtime
LEFT JOIN mimiciv_derived.ventilation AS vd
ON ie.stay_id = vd.stay_id
AND bg.charttime >= vd.starttime
AND bg.charttime <= vd.endtime
AND vd.ventilation_status = 'InvasiveVent'
LEFT JOIN cpap AS cp
ON ie.stay_id = cp.stay_id AND bg.charttime >= cp.starttime AND bg.charttime <= cp.endtime
ON ie.stay_id = cp.stay_id
AND bg.charttime >= cp.starttime
AND bg.charttime <= cp.endtime
), pafi2 AS (
SELECT
stay_id,
Expand Down
22 changes: 17 additions & 5 deletions mimic-iv/concepts_duckdb/score/sapsii.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ WITH co AS (
MAX(CASE WHEN REGEXP_MATCHES(LOWER(ce.value), '(cpap mask|bipap)') THEN 1 ELSE 0 END) AS cpap
FROM co
INNER JOIN mimiciv_icu.chartevents AS ce
ON co.stay_id = ce.stay_id AND ce.charttime > co.starttime AND ce.charttime <= co.endtime
ON co.stay_id = ce.stay_id
AND ce.charttime > co.starttime
AND ce.charttime <= co.endtime
WHERE
ce.itemid = 226732 AND REGEXP_MATCHES(LOWER(ce.value), '(cpap mask|bipap)')
GROUP BY
Expand Down Expand Up @@ -135,7 +137,9 @@ WITH co AS (
MIN(gcs.gcs) AS mingcs
FROM co
LEFT JOIN mimiciv_derived.gcs AS gcs
ON co.stay_id = gcs.stay_id AND co.starttime < gcs.charttime AND gcs.charttime <= co.endtime
ON co.stay_id = gcs.stay_id
AND co.starttime < gcs.charttime
AND gcs.charttime <= co.endtime
GROUP BY
co.stay_id
), vital AS (
Expand All @@ -160,7 +164,9 @@ WITH co AS (
SUM(uo.urineoutput) AS urineoutput
FROM co
LEFT JOIN mimiciv_derived.urine_output AS uo
ON co.stay_id = uo.stay_id AND co.starttime < uo.charttime AND co.endtime >= uo.charttime
ON co.stay_id = uo.stay_id
AND co.starttime < uo.charttime
AND co.endtime >= uo.charttime
GROUP BY
co.stay_id
), labs AS (
Expand Down Expand Up @@ -301,7 +307,10 @@ WITH co AS (
THEN 4
WHEN heartrate_min < 70
THEN 2
WHEN heartrate_max >= 70 AND heartrate_max < 120 AND heartrate_min >= 70 AND heartrate_min < 120
WHEN heartrate_max >= 70
AND heartrate_max < 120
AND heartrate_min >= 70
AND heartrate_min < 120
THEN 0
END AS hr_score,
CASE
Expand Down Expand Up @@ -371,7 +380,10 @@ WITH co AS (
THEN 3
WHEN potassium_max >= 5.0
THEN 3
WHEN potassium_max >= 3.0 AND potassium_max < 5.0 AND potassium_min >= 3.0 AND potassium_min < 5.0
WHEN potassium_max >= 3.0
AND potassium_max < 5.0
AND potassium_min >= 3.0
AND potassium_min < 5.0
THEN 0
END AS potassium_score,
CASE
Expand Down
36 changes: 27 additions & 9 deletions mimic-iv/concepts_duckdb/score/sofa.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ WITH co AS (
MIN(vs.mbp) AS meanbp_min
FROM co
LEFT JOIN mimiciv_derived.vitalsign AS vs
ON co.stay_id = vs.stay_id AND co.starttime < vs.charttime AND co.endtime >= vs.charttime
ON co.stay_id = vs.stay_id
AND co.starttime < vs.charttime
AND co.endtime >= vs.charttime
GROUP BY
co.stay_id,
co.hr
Expand All @@ -44,7 +46,9 @@ WITH co AS (
MIN(gcs.gcs) AS gcs_min
FROM co
LEFT JOIN mimiciv_derived.gcs AS gcs
ON co.stay_id = gcs.stay_id AND co.starttime < gcs.charttime AND co.endtime >= gcs.charttime
ON co.stay_id = gcs.stay_id
AND co.starttime < gcs.charttime
AND co.endtime >= gcs.charttime
GROUP BY
co.stay_id,
co.hr
Expand All @@ -55,7 +59,9 @@ WITH co AS (
MAX(enz.bilirubin_total) AS bilirubin_max
FROM co
LEFT JOIN mimiciv_derived.enzyme AS enz
ON co.hadm_id = enz.hadm_id AND co.starttime < enz.charttime AND co.endtime >= enz.charttime
ON co.hadm_id = enz.hadm_id
AND co.starttime < enz.charttime
AND co.endtime >= enz.charttime
GROUP BY
co.stay_id,
co.hr
Expand All @@ -79,7 +85,9 @@ WITH co AS (
MIN(cbc.platelet) AS platelet_min
FROM co
LEFT JOIN mimiciv_derived.complete_blood_count AS cbc
ON co.hadm_id = cbc.hadm_id AND co.starttime < cbc.charttime AND co.endtime >= cbc.charttime
ON co.hadm_id = cbc.hadm_id
AND co.starttime < cbc.charttime
AND co.endtime >= cbc.charttime
GROUP BY
co.stay_id,
co.hr
Expand Down Expand Up @@ -109,7 +117,9 @@ WITH co AS (
) AS uo_24hr
FROM co
LEFT JOIN mimiciv_derived.urine_output_rate AS uo
ON co.stay_id = uo.stay_id AND co.starttime < uo.charttime AND co.endtime >= uo.charttime
ON co.stay_id = uo.stay_id
AND co.starttime < uo.charttime
AND co.endtime >= uo.charttime
GROUP BY
co.stay_id,
co.hr
Expand All @@ -123,13 +133,21 @@ WITH co AS (
MAX(dob.vaso_rate) AS rate_dobutamine
FROM co
LEFT JOIN mimiciv_derived.epinephrine AS epi
ON co.stay_id = epi.stay_id AND co.endtime > epi.starttime AND co.endtime <= epi.endtime
ON co.stay_id = epi.stay_id
AND co.endtime > epi.starttime
AND co.endtime <= epi.endtime
LEFT JOIN mimiciv_derived.norepinephrine AS nor
ON co.stay_id = nor.stay_id AND co.endtime > nor.starttime AND co.endtime <= nor.endtime
ON co.stay_id = nor.stay_id
AND co.endtime > nor.starttime
AND co.endtime <= nor.endtime
LEFT JOIN mimiciv_derived.dopamine AS dop
ON co.stay_id = dop.stay_id AND co.endtime > dop.starttime AND co.endtime <= dop.endtime
ON co.stay_id = dop.stay_id
AND co.endtime > dop.starttime
AND co.endtime <= dop.endtime
LEFT JOIN mimiciv_derived.dobutamine AS dob
ON co.stay_id = dob.stay_id AND co.endtime > dob.starttime AND co.endtime <= dob.endtime
ON co.stay_id = dob.stay_id
AND co.endtime > dob.starttime
AND co.endtime <= dob.endtime
WHERE
NOT epi.stay_id IS NULL
OR NOT nor.stay_id IS NULL
Expand Down

0 comments on commit beeb69b

Please sign in to comment.