From 27f75b83495cfbece57ec1dfd79357303210af59 Mon Sep 17 00:00:00 2001 From: aegis301 <57251443+aegis301@users.noreply.github.com> Date: Sat, 24 Feb 2024 21:53:09 +0100 Subject: [PATCH] feat: implementation of fluid balance --- .../measurement/daily_fluid_balance.sql | 16 ++++++++++++++++ .../measurement/daily_fluid_in.sql | 18 ++++++++++++++++++ .../measurement/daily_fluid_out.sql | 14 ++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 mimic-iv/concepts_postgres/measurement/daily_fluid_balance.sql create mode 100644 mimic-iv/concepts_postgres/measurement/daily_fluid_in.sql create mode 100644 mimic-iv/concepts_postgres/measurement/daily_fluid_out.sql diff --git a/mimic-iv/concepts_postgres/measurement/daily_fluid_balance.sql b/mimic-iv/concepts_postgres/measurement/daily_fluid_balance.sql new file mode 100644 index 00000000..4edd7566 --- /dev/null +++ b/mimic-iv/concepts_postgres/measurement/daily_fluid_balance.sql @@ -0,0 +1,16 @@ +DROP TABLE IF EXISTS + mimiciv_derived.daily_fluid_balance +CASCADE; + +CREATE TABLE + mimiciv_derived.daily_fluid_balance AS +SELECT + i.stay_id, + i.infusion_date, + ( + i.total_daily_amount - COALESCE(o.total_output, 0) + ) as net_balance +FROM + mimiciv_derived.daily_fluid_in AS i + LEFT JOIN mimiciv_derived.daily_fluid_out AS o ON i.stay_id = o.stay_id + AND i.infusion_date = o.day; \ No newline at end of file diff --git a/mimic-iv/concepts_postgres/measurement/daily_fluid_in.sql b/mimic-iv/concepts_postgres/measurement/daily_fluid_in.sql new file mode 100644 index 00000000..9b44b692 --- /dev/null +++ b/mimic-iv/concepts_postgres/measurement/daily_fluid_in.sql @@ -0,0 +1,18 @@ +DROP TABLE IF EXISTS mimiciv_derived.daily_fluid_in +CASCADE; +CREATE TABLE mimiciv_derived.daily_fluid_in AS +WITH RECURSIVE infusion_days AS +( + SELECT stay_id, + CAST(starttime AS DATE) AS infusion_date, + CASE + WHEN CAST(starttime AS DATE) = CAST(endtime AS DATE) THEN amount + ELSE amount / EXTRACT(EPOCH FROM (endtime - starttime)) / 3600 * (24 - EXTRACT(HOUR FROM starttime)) + END AS daily_amount, + endtime +FROM mimiciv_icu.inputevents +) +SELECT stay_id, infusion_date, SUM(daily_amount) AS total_daily_amount +FROM infusion_days +GROUP BY stay_id, infusion_date +ORDER BY stay_id, infusion_date; \ No newline at end of file diff --git a/mimic-iv/concepts_postgres/measurement/daily_fluid_out.sql b/mimic-iv/concepts_postgres/measurement/daily_fluid_out.sql new file mode 100644 index 00000000..7dde1bb3 --- /dev/null +++ b/mimic-iv/concepts_postgres/measurement/daily_fluid_out.sql @@ -0,0 +1,14 @@ +DROP TABLE IF EXISTS mimiciv_derived.daily_fluid_out; +CREATE TABLE mimiciv_derived.daily_fluid_out AS +SELECT + stay_id, + DATE(charttime) as day, + SUM(value) as total_output +FROM + mimiciv_icu.outputevents +GROUP BY + stay_id, + day +ORDER BY + stay_id, + day; \ No newline at end of file