diff --git a/.github/workflows/lint_sqlfluff.yml b/.github/workflows/lint_sqlfluff.yml index 9401198e..ca4c8c8b 100644 --- a/.github/workflows/lint_sqlfluff.yml +++ b/.github/workflows/lint_sqlfluff.yml @@ -35,8 +35,12 @@ jobs: shell: bash run: sqlfluff lint --format github-annotation --annotation-level failure --nofail ${{ steps.get_files_to_lint.outputs.lintees }} > annotations.json - name: Annotate + # Fork PRs cannot create check runs with GITHUB_TOKEN; lint already ran. + continue-on-error: true + if: steps.get_files_to_lint.outputs.lintees != '' uses: yuzutech/annotations-action@v0.6.0 with: repo-token: "${{ secrets.GITHUB_TOKEN }}" title: "SQLFluff Lint" - input: "./annotations.json" \ No newline at end of file + input: "./annotations.json" + ignore-missing-file: true \ No newline at end of file diff --git a/mimic-iii/concepts/comorbidity/elixhauser_ahrq_v37_no_drg.sql b/mimic-iii/concepts/comorbidity/elixhauser_ahrq_v37_no_drg.sql index c9f10a78..a2742ea2 100644 --- a/mimic-iii/concepts/comorbidity/elixhauser_ahrq_v37_no_drg.sql +++ b/mimic-iii/concepts/comorbidity/elixhauser_ahrq_v37_no_drg.sql @@ -5,8 +5,15 @@ -- The code: -- removes "primary" ICD9_CODE (seq_num != 1) -- uses AHRQ published rules to define comorbidities +-- RTRIM icd9_code so space-padded values still match BETWEEN/equality with -eliflg as +icd as +( + select hadm_id, seq_num, RTRIM(icd9_code) as icd9_code + from `physionet-data.mimiciii_clinical.diagnoses_icd` + where seq_num != 1 +) +, eliflg as ( select hadm_id, seq_num, icd9_code -- note that these codes will seem incomplete at first @@ -390,8 +397,7 @@ select hadm_id, seq_num, icd9_code when icd9_code = '3091' then 1 when icd9_code = '311' then 1 end as depress /* Depression */ -from `physionet-data.mimiciii_clinical.diagnoses_icd` icd -WHERE seq_num = 1 +from icd ) -- collapse the icd9_code specific flags into hadm_id specific flags -- this groups comorbidities together for a single patient admission diff --git a/mimic-iii/concepts/pivot/pivoted_sofa.sql b/mimic-iii/concepts/pivot/pivoted_sofa.sql index 6d4940fd..c7947368 100644 --- a/mimic-iii/concepts/pivot/pivoted_sofa.sql +++ b/mimic-iii/concepts/pivot/pivoted_sofa.sql @@ -228,7 +228,7 @@ WITH co AS -- Cardiovascular , cast(case when rate_dopamine > 15 or rate_epinephrine > 0.1 or rate_norepinephrine > 0.1 then 4 - when rate_dopamine > 5 or rate_epinephrine <= 0.1 or rate_norepinephrine <= 0.1 then 3 + when rate_dopamine > 5 or (rate_epinephrine > 0 and rate_epinephrine <= 0.1) or (rate_norepinephrine > 0 and rate_norepinephrine <= 0.1) then 3 when rate_dopamine > 0 or rate_dobutamine > 0 then 2 when meanbp_min < 70 then 1 when coalesce(meanbp_min, rate_dopamine, rate_dobutamine, rate_epinephrine, rate_norepinephrine) is null then null diff --git a/mimic-iii/concepts/severityscores/sofa.sql b/mimic-iii/concepts/severityscores/sofa.sql index 9afc5be1..52cb8d5f 100644 --- a/mimic-iii/concepts/severityscores/sofa.sql +++ b/mimic-iii/concepts/severityscores/sofa.sql @@ -218,7 +218,7 @@ left join `physionet-data.mimiciii_derived.gcs_first_day` gcs -- Cardiovascular , case when rate_dopamine > 15 or rate_epinephrine > 0.1 or rate_norepinephrine > 0.1 then 4 - when rate_dopamine > 5 or rate_epinephrine <= 0.1 or rate_norepinephrine <= 0.1 then 3 + when rate_dopamine > 5 or (rate_epinephrine > 0 and rate_epinephrine <= 0.1) or (rate_norepinephrine > 0 and rate_norepinephrine <= 0.1) then 3 when rate_dopamine > 0 or rate_dobutamine > 0 then 2 when meanbp_min < 70 then 1 when coalesce(meanbp_min, rate_dopamine, rate_dobutamine, rate_epinephrine, rate_norepinephrine) is null then null diff --git a/mimic-iii/concepts_duckdb/comorbidity/elixhauser_ahrq_v37_no_drg.sql b/mimic-iii/concepts_duckdb/comorbidity/elixhauser_ahrq_v37_no_drg.sql index 779d7f43..a7d42599 100644 --- a/mimic-iii/concepts_duckdb/comorbidity/elixhauser_ahrq_v37_no_drg.sql +++ b/mimic-iii/concepts_duckdb/comorbidity/elixhauser_ahrq_v37_no_drg.sql @@ -1,6 +1,14 @@ -- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY. DROP TABLE IF EXISTS mimiciii_derived.elixhauser_ahrq_v37_no_drg; CREATE TABLE mimiciii_derived.elixhauser_ahrq_v37_no_drg AS -WITH eliflg AS ( +WITH icd AS ( + SELECT + hadm_id, + seq_num, + RTRIM(icd9_code) AS icd9_code + FROM mimiciii.diagnoses_icd + WHERE + seq_num <> 1 +), eliflg AS ( SELECT hadm_id, seq_num, @@ -559,9 +567,7 @@ WITH eliflg AS ( WHEN icd9_code = '311' THEN 1 END AS depress - FROM mimiciii.diagnoses_icd AS icd - WHERE - seq_num = 1 + FROM icd ), eligrp AS ( SELECT hadm_id, diff --git a/mimic-iii/concepts_duckdb/pivot/pivoted_sofa.sql b/mimic-iii/concepts_duckdb/pivot/pivoted_sofa.sql index 78ac4fe5..283c15da 100644 --- a/mimic-iii/concepts_duckdb/pivot/pivoted_sofa.sql +++ b/mimic-iii/concepts_duckdb/pivot/pivoted_sofa.sql @@ -175,7 +175,13 @@ WITH co AS ( CAST(CASE WHEN rate_dopamine > 15 OR rate_epinephrine > 0.1 OR rate_norepinephrine > 0.1 THEN 4 - WHEN rate_dopamine > 5 OR rate_epinephrine <= 0.1 OR rate_norepinephrine <= 0.1 + WHEN rate_dopamine > 5 + OR ( + rate_epinephrine > 0 AND rate_epinephrine <= 0.1 + ) + OR ( + rate_norepinephrine > 0 AND rate_norepinephrine <= 0.1 + ) THEN 3 WHEN rate_dopamine > 0 OR rate_dobutamine > 0 THEN 2 diff --git a/mimic-iii/concepts_duckdb/severityscores/sofa.sql b/mimic-iii/concepts_duckdb/severityscores/sofa.sql index 7bba85e9..2c8a7c1e 100644 --- a/mimic-iii/concepts_duckdb/severityscores/sofa.sql +++ b/mimic-iii/concepts_duckdb/severityscores/sofa.sql @@ -186,7 +186,13 @@ WITH wt AS ( CASE WHEN rate_dopamine > 15 OR rate_epinephrine > 0.1 OR rate_norepinephrine > 0.1 THEN 4 - WHEN rate_dopamine > 5 OR rate_epinephrine <= 0.1 OR rate_norepinephrine <= 0.1 + WHEN rate_dopamine > 5 + OR ( + rate_epinephrine > 0 AND rate_epinephrine <= 0.1 + ) + OR ( + rate_norepinephrine > 0 AND rate_norepinephrine <= 0.1 + ) THEN 3 WHEN rate_dopamine > 0 OR rate_dobutamine > 0 THEN 2 diff --git a/mimic-iii/concepts_postgres/comorbidity/elixhauser_ahrq_v37_no_drg.sql b/mimic-iii/concepts_postgres/comorbidity/elixhauser_ahrq_v37_no_drg.sql index 5bfd7e3a..cb2cae75 100644 --- a/mimic-iii/concepts_postgres/comorbidity/elixhauser_ahrq_v37_no_drg.sql +++ b/mimic-iii/concepts_postgres/comorbidity/elixhauser_ahrq_v37_no_drg.sql @@ -1,7 +1,21 @@ -- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY. DROP TABLE IF EXISTS mimiciii_derived.elixhauser_ahrq_v37_no_drg; CREATE TABLE mimiciii_derived.elixhauser_ahrq_v37_no_drg AS -/* This code uses the latest version of Elixhauser provided by AHRQ */ /* However, it does *not* filter based on diagnosis related groups (DRGs) */ /* As such, "comorbidities" identified are more likely to be associated with the primary reason for their hospital stay */ /* The code: */ /* removes "primary" ICD9_CODE (seq_num != 1) */ /* uses AHRQ published rules to define comorbidities */ -WITH eliflg AS ( +/* This code uses the latest version of Elixhauser provided by AHRQ */ +/* However, it does *not* filter based on diagnosis related groups (DRGs) */ +/* As such, "comorbidities" identified are more likely to be associated with the primary reason for their hospital stay */ +/* The code: */ +/* removes "primary" ICD9_CODE (seq_num != 1) */ +/* uses AHRQ published rules to define comorbidities */ +/* RTRIM icd9_code so space-padded values still match BETWEEN/equality */ +WITH icd AS ( + SELECT + hadm_id, + seq_num, + RTRIM(icd9_code) AS icd9_code + FROM mimiciii.diagnoses_icd + WHERE + seq_num <> 1 +), eliflg AS ( SELECT hadm_id, seq_num, @@ -562,9 +576,7 @@ WITH eliflg AS ( WHEN icd9_code = '311' THEN 1 END AS depress /* Depression */ - FROM mimiciii.diagnoses_icd AS icd - WHERE - seq_num = 1 + FROM icd ), eligrp /* collapse the icd9_code specific flags into hadm_id specific flags */ /* this groups comorbidities together for a single patient admission */ AS ( SELECT hadm_id, @@ -613,7 +625,8 @@ WITH eliflg AS ( GROUP BY hadm_id ) -/* now merge these flags together to define elixhauser */ /* most are straightforward.. but hypertension flags are a bit more complicated */ +/* now merge these flags together to define elixhauser */ +/* most are straightforward.. but hypertension flags are a bit more complicated */ SELECT adm.subject_id, adm.hadm_id, diff --git a/mimic-iii/concepts_postgres/pivot/pivoted_sofa.sql b/mimic-iii/concepts_postgres/pivot/pivoted_sofa.sql index 3aa38e21..47ccb27c 100644 --- a/mimic-iii/concepts_postgres/pivot/pivoted_sofa.sql +++ b/mimic-iii/concepts_postgres/pivot/pivoted_sofa.sql @@ -1,6 +1,36 @@ -- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY. DROP TABLE IF EXISTS mimiciii_derived.pivoted_sofa; CREATE TABLE mimiciii_derived.pivoted_sofa AS -/* ------------------------------------------------------------------ */ /* Title: Sequential Organ Failure Assessment (SOFA) */ /* This query extracts the sequential organ failure assessment (formally: sepsis-related organ failure assessment). */ /* This score is a measure of organ failure for patients in the ICU. */ /* The score is calculated for every hour of the patient's ICU stay. */ /* However, as the calculation window is 24 hours, care should be taken when */ /* using the score before the end of the first day. */ /* ------------------------------------------------------------------ */ /* Reference for SOFA: */ /* Jean-Louis Vincent, Rui Moreno, Jukka Takala, Sheila Willatts, Arnaldo De Mendonça, */ /* Hajo Bruining, C. K. Reinhart, Peter M Suter, and L. G. Thijs. */ /* "The SOFA (Sepsis-related Organ Failure Assessment) score to describe organ dysfunction/failure." */ /* Intensive care medicine 22, no. 7 (1996): 707-710. */ /* Variables used in SOFA: */ /* GCS, MAP, FiO2, Ventilation status (sourced FROM `physionet-data.mimiciii_clinical.chartevents`) */ /* Creatinine, Bilirubin, FiO2, PaO2, Platelets (sourced FROM `physionet-data.mimiciii_clinical.labevents`) */ /* Dopamine, Dobutamine, Epinephrine, Norepinephrine (sourced FROM `physionet-data.mimiciii_clinical.inputevents_mv` and INPUTEVENTS_CV) */ /* Urine output (sourced from OUTPUTEVENTS) */ /* The following views required to run this query: */ /* 1) pivoted_bg_art - generated by pivoted-bg.sql */ /* 2) pivoted_uo - generated by pivoted-uo.sql */ /* 3) pivoted_lab - generated by pivoted-lab.sql */ /* 4) pivoted_gcs - generated by pivoted-gcs.sql */ /* 5) ventilation_durations - generated by ../durations/ventilation_durations.sql */ /* 6) norepinephrine_dose - generated by ../durations/norepinephrine-dose.sql */ /* 7) epinephrine_dose - generated by ../durations/epinephrine-dose.sql */ /* 8) dopamine_dose - generated by ../durations/dopamine-dose.sql */ /* 9) dobutamine_dose - generated by ../durations/dobutamine-dose.sql */ /* Note: */ /* The score is calculated for only adult ICU patients, */ /* generate a row for every hour the patient was in the ICU */ +/* ------------------------------------------------------------------ */ +/* Title: Sequential Organ Failure Assessment (SOFA) */ +/* This query extracts the sequential organ failure assessment (formally: sepsis-related organ failure assessment). */ +/* This score is a measure of organ failure for patients in the ICU. */ +/* The score is calculated for every hour of the patient's ICU stay. */ +/* However, as the calculation window is 24 hours, care should be taken when */ +/* using the score before the end of the first day. */ +/* ------------------------------------------------------------------ */ +/* Reference for SOFA: */ +/* Jean-Louis Vincent, Rui Moreno, Jukka Takala, Sheila Willatts, Arnaldo De Mendonça, */ +/* Hajo Bruining, C. K. Reinhart, Peter M Suter, and L. G. Thijs. */ +/* "The SOFA (Sepsis-related Organ Failure Assessment) score to describe organ dysfunction/failure." */ +/* Intensive care medicine 22, no. 7 (1996): 707-710. */ +/* Variables used in SOFA: */ +/* GCS, MAP, FiO2, Ventilation status (sourced FROM `physionet-data.mimiciii_clinical.chartevents`) */ +/* Creatinine, Bilirubin, FiO2, PaO2, Platelets (sourced FROM `physionet-data.mimiciii_clinical.labevents`) */ +/* Dopamine, Dobutamine, Epinephrine, Norepinephrine (sourced FROM `physionet-data.mimiciii_clinical.inputevents_mv` and INPUTEVENTS_CV) */ +/* Urine output (sourced from OUTPUTEVENTS) */ +/* The following views required to run this query: */ +/* 1) pivoted_bg_art - generated by pivoted-bg.sql */ +/* 2) pivoted_uo - generated by pivoted-uo.sql */ +/* 3) pivoted_lab - generated by pivoted-lab.sql */ +/* 4) pivoted_gcs - generated by pivoted-gcs.sql */ +/* 5) ventilation_durations - generated by ../durations/ventilation_durations.sql */ +/* 6) norepinephrine_dose - generated by ../durations/norepinephrine-dose.sql */ +/* 7) epinephrine_dose - generated by ../durations/epinephrine-dose.sql */ +/* 8) dopamine_dose - generated by ../durations/dopamine-dose.sql */ +/* 9) dobutamine_dose - generated by ../durations/dobutamine-dose.sql */ +/* Note: */ +/* The score is calculated for only adult ICU patients, */ +/* generate a row for every hour the patient was in the ICU */ WITH co AS ( SELECT ih.icustay_id, @@ -144,7 +174,9 @@ WITH co AS ( AND co.endtime > dob.starttime AND co.endtime <= dob.endtime ), scorecalc AS ( - /* Calculate the final score */ /* note that if the underlying data is missing, the component is null */ /* eventually these are treated as 0 (normal), but knowing when data is missing is useful for debugging */ + /* Calculate the final score */ + /* note that if the underlying data is missing, the component is null */ + /* eventually these are treated as 0 (normal), but knowing when data is missing is useful for debugging */ SELECT scorecomp.*, /* Respiration */ CAST(CASE @@ -189,7 +221,13 @@ WITH co AS ( CAST(CASE WHEN rate_dopamine > 15 OR rate_epinephrine > 0.1 OR rate_norepinephrine > 0.1 THEN 4 - WHEN rate_dopamine > 5 OR rate_epinephrine <= 0.1 OR rate_norepinephrine <= 0.1 + WHEN rate_dopamine > 5 + OR ( + rate_epinephrine > 0 AND rate_epinephrine <= 0.1 + ) + OR ( + rate_norepinephrine > 0 AND rate_norepinephrine <= 0.1 + ) THEN 3 WHEN rate_dopamine > 0 OR rate_dobutamine > 0 THEN 2 diff --git a/mimic-iii/concepts_postgres/severityscores/sofa.sql b/mimic-iii/concepts_postgres/severityscores/sofa.sql index ebd63ea4..2f82e693 100644 --- a/mimic-iii/concepts_postgres/severityscores/sofa.sql +++ b/mimic-iii/concepts_postgres/severityscores/sofa.sql @@ -1,6 +1,32 @@ -- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY. DROP TABLE IF EXISTS mimiciii_derived.sofa; CREATE TABLE mimiciii_derived.sofa AS -/* ------------------------------------------------------------------ */ /* Title: Sequential Organ Failure Assessment (SOFA) */ /* This query extracts the sequential organ failure assessment (formally: sepsis-related organ failure assessment). */ /* This score is a measure of organ failure for patients in the ICU. */ /* The score is calculated on the first day of each ICU patients' stay. */ /* ------------------------------------------------------------------ */ /* Reference for SOFA: */ /* Jean-Louis Vincent, Rui Moreno, Jukka Takala, Sheila Willatts, Arnaldo De Mendonça, */ /* Hajo Bruining, C. K. Reinhart, Peter M Suter, and L. G. Thijs. */ /* "The SOFA (Sepsis-related Organ Failure Assessment) score to describe organ dysfunction/failure." */ /* Intensive care medicine 22, no. 7 (1996): 707-710. */ /* Variables used in SOFA: */ /* GCS, MAP, FiO2, Ventilation status (sourced FROM `physionet-data.mimiciii_clinical.chartevents`) */ /* Creatinine, Bilirubin, FiO2, PaO2, Platelets (sourced FROM `physionet-data.mimiciii_clinical.labevents`) */ /* Dobutamine, Epinephrine, Norepinephrine (sourced FROM `physionet-data.mimiciii_clinical.inputevents_mv` and INPUTEVENTS_CV) */ /* Urine output (sourced from OUTPUTEVENTS) */ /* The following views required to run this query: */ /* 1) urine_output_first_day - generated by urine-output-first-day.sql */ /* 2) vitals_first_day - generated by vitals-first-day.sql */ /* 3) gcs_first_day - generated by gcs-first-day.sql */ /* 4) labs_first_day - generated by labs-first-day.sql */ /* 5) blood_gas_first_day_arterial - generated by blood-gas-first-day-arterial.sql */ /* 6) echodata - generated by echo-data.sql */ /* 7) ventilation_durations - generated by ventilation_durations.sql */ /* Note: */ /* The score is calculated for *all* ICU patients, with the assumption that the user will subselect appropriate ICUSTAY_IDs. */ /* For example, the score is calculated for neonates, but it is likely inappropriate to actually use the score values for these patients. */ +/* ------------------------------------------------------------------ */ +/* Title: Sequential Organ Failure Assessment (SOFA) */ +/* This query extracts the sequential organ failure assessment (formally: sepsis-related organ failure assessment). */ +/* This score is a measure of organ failure for patients in the ICU. */ +/* The score is calculated on the first day of each ICU patients' stay. */ +/* ------------------------------------------------------------------ */ +/* Reference for SOFA: */ +/* Jean-Louis Vincent, Rui Moreno, Jukka Takala, Sheila Willatts, Arnaldo De Mendonça, */ +/* Hajo Bruining, C. K. Reinhart, Peter M Suter, and L. G. Thijs. */ +/* "The SOFA (Sepsis-related Organ Failure Assessment) score to describe organ dysfunction/failure." */ +/* Intensive care medicine 22, no. 7 (1996): 707-710. */ +/* Variables used in SOFA: */ +/* GCS, MAP, FiO2, Ventilation status (sourced FROM `physionet-data.mimiciii_clinical.chartevents`) */ +/* Creatinine, Bilirubin, FiO2, PaO2, Platelets (sourced FROM `physionet-data.mimiciii_clinical.labevents`) */ +/* Dobutamine, Epinephrine, Norepinephrine (sourced FROM `physionet-data.mimiciii_clinical.inputevents_mv` and INPUTEVENTS_CV) */ +/* Urine output (sourced from OUTPUTEVENTS) */ +/* The following views required to run this query: */ +/* 1) urine_output_first_day - generated by urine-output-first-day.sql */ +/* 2) vitals_first_day - generated by vitals-first-day.sql */ +/* 3) gcs_first_day - generated by gcs-first-day.sql */ +/* 4) labs_first_day - generated by labs-first-day.sql */ +/* 5) blood_gas_first_day_arterial - generated by blood-gas-first-day-arterial.sql */ +/* 6) echodata - generated by echo-data.sql */ +/* 7) ventilation_durations - generated by ventilation_durations.sql */ +/* Note: */ +/* The score is calculated for *all* ICU patients, with the assumption that the user will subselect appropriate ICUSTAY_IDs. */ +/* For example, the score is calculated for neonates, but it is likely inappropriate to actually use the score values for these patients. */ WITH wt AS ( SELECT ie.icustay_id, /* ensure weight is measured in kg */ @@ -115,7 +141,9 @@ WITH wt AS ( bg.icustay_id NULLS FIRST, bg.charttime NULLS FIRST ), pafi2 AS ( - /* because pafi has an interaction between vent/PaO2:FiO2, we need two columns for the score */ /* it can happen that the lowest unventilated PaO2/FiO2 is 68, but the lowest ventilated PaO2/FiO2 is 120 */ /* in this case, the SOFA score is 3, *not* 4. */ + /* because pafi has an interaction between vent/PaO2:FiO2, we need two columns for the score */ + /* it can happen that the lowest unventilated PaO2/FiO2 is 68, but the lowest ventilated PaO2/FiO2 is 120 */ + /* in this case, the SOFA score is 3, *not* 4. */ SELECT icustay_id, MIN(CASE WHEN isvent = 0 THEN pao2fio2 ELSE NULL END) AS pao2fio2_novent_min, @@ -154,7 +182,9 @@ WITH wt AS ( LEFT JOIN mimiciii_derived.gcs_first_day AS gcs ON ie.icustay_id = gcs.icustay_id ), scorecalc AS ( - /* Calculate the final score */ /* note that if the underlying data is missing, the component is null */ /* eventually these are treated as 0 (normal), but knowing when data is missing is useful for debugging */ + /* Calculate the final score */ + /* note that if the underlying data is missing, the component is null */ + /* eventually these are treated as 0 (normal), but knowing when data is missing is useful for debugging */ SELECT icustay_id, /* Respiration */ CASE @@ -199,7 +229,13 @@ WITH wt AS ( CASE WHEN rate_dopamine > 15 OR rate_epinephrine > 0.1 OR rate_norepinephrine > 0.1 THEN 4 - WHEN rate_dopamine > 5 OR rate_epinephrine <= 0.1 OR rate_norepinephrine <= 0.1 + WHEN rate_dopamine > 5 + OR ( + rate_epinephrine > 0 AND rate_epinephrine <= 0.1 + ) + OR ( + rate_norepinephrine > 0 AND rate_norepinephrine <= 0.1 + ) THEN 3 WHEN rate_dopamine > 0 OR rate_dobutamine > 0 THEN 2 diff --git a/mimic-iv/concepts/firstday/first_day_sofa.sql b/mimic-iv/concepts/firstday/first_day_sofa.sql index 62282af7..ffd3d3c2 100644 --- a/mimic-iv/concepts/firstday/first_day_sofa.sql +++ b/mimic-iv/concepts/firstday/first_day_sofa.sql @@ -196,8 +196,8 @@ WITH vaso_stg AS ( OR rate_norepinephrine > 0.1 THEN 4 WHEN rate_dopamine > 5 - OR rate_epinephrine <= 0.1 - OR rate_norepinephrine <= 0.1 + OR (rate_epinephrine > 0 AND rate_epinephrine <= 0.1) + OR (rate_norepinephrine > 0 AND rate_norepinephrine <= 0.1) THEN 3 WHEN rate_dopamine > 0 OR rate_dobutamine > 0 THEN 2 WHEN mbp_min < 70 THEN 1 diff --git a/mimic-iv/concepts/measurement/urine_output_rate.sql b/mimic-iv/concepts/measurement/urine_output_rate.sql index 01939211..25964434 100644 --- a/mimic-iv/concepts/measurement/urine_output_rate.sql +++ b/mimic-iv/concepts/measurement/urine_output_rate.sql @@ -16,12 +16,14 @@ WITH tm AS ( ) -- now calculate time since last UO measurement +-- Use SECOND then convert so BigQuery (truncating) and Postgres (fractional) +-- DATETIME_DIFF agree; same approach as kdigo_uo.sql. , uo_tm AS ( SELECT tm.stay_id , CASE WHEN LAG(charttime) OVER w IS NULL - THEN DATETIME_DIFF(charttime, intime_hr, MINUTE) - ELSE DATETIME_DIFF(charttime, LAG(charttime) OVER w, MINUTE) + THEN DATETIME_DIFF(charttime, intime_hr, SECOND) / 60.0 + ELSE DATETIME_DIFF(charttime, LAG(charttime) OVER w, SECOND) / 60.0 END AS tm_since_last_uo , uo.charttime , uo.urineoutput @@ -45,26 +47,37 @@ WITH tm AS ( -- to 1 hour of UO, therefore we use '5' and '11' to restrict the -- period, rather than 6/12 this assumption may overestimate UO rate -- when documentation is done less than hourly - , SUM(CASE WHEN DATETIME_DIFF(io.charttime, iosum.charttime, HOUR) <= 5 + , SUM(CASE + WHEN DATETIME_DIFF( + io.charttime, iosum.charttime, SECOND + ) / 3600.0 <= 5 THEN iosum.urineoutput ELSE NULL END) AS urineoutput_6hr , ROUND(CAST( SUM( CASE - WHEN DATETIME_DIFF(io.charttime, iosum.charttime, HOUR) <= 5 - THEN iosum.tm_since_last_uo - ELSE NULL END) / 60.0 AS NUMERIC + WHEN DATETIME_DIFF( + io.charttime, iosum.charttime, SECOND + ) / 3600.0 <= 5 + THEN iosum.tm_since_last_uo + ELSE NULL END + ) / 60.0 AS NUMERIC ), 6) AS uo_tm_6hr - , SUM(CASE WHEN DATETIME_DIFF(io.charttime, iosum.charttime, HOUR) <= 11 + , SUM(CASE + WHEN DATETIME_DIFF( + io.charttime, iosum.charttime, SECOND + ) / 3600.0 <= 11 THEN iosum.urineoutput ELSE NULL END) AS urineoutput_12hr , ROUND(CAST( SUM( CASE - WHEN - DATETIME_DIFF(io.charttime, iosum.charttime, HOUR) <= 11 - THEN iosum.tm_since_last_uo - ELSE NULL END) / 60.0 AS NUMERIC + WHEN DATETIME_DIFF( + io.charttime, iosum.charttime, SECOND + ) / 3600.0 <= 11 + THEN iosum.tm_since_last_uo + ELSE NULL END + ) / 60.0 AS NUMERIC ), 6) AS uo_tm_12hr -- 24 hours , SUM(iosum.urineoutput) AS urineoutput_24hr diff --git a/mimic-iv/concepts/score/sofa.sql b/mimic-iv/concepts/score/sofa.sql index 0dbf28d1..049b3920 100644 --- a/mimic-iv/concepts/score/sofa.sql +++ b/mimic-iv/concepts/score/sofa.sql @@ -280,8 +280,8 @@ WITH co AS ( OR rate_norepinephrine > 0.1 THEN 4 WHEN rate_dopamine > 5 - OR rate_epinephrine <= 0.1 - OR rate_norepinephrine <= 0.1 + OR (rate_epinephrine > 0 AND rate_epinephrine <= 0.1) + OR (rate_norepinephrine > 0 AND rate_norepinephrine <= 0.1) THEN 3 WHEN rate_dopamine > 0 OR rate_dobutamine > 0 diff --git a/mimic-iv/concepts_duckdb/firstday/first_day_sofa.sql b/mimic-iv/concepts_duckdb/firstday/first_day_sofa.sql index ea3305a7..07cae6a6 100644 --- a/mimic-iv/concepts_duckdb/firstday/first_day_sofa.sql +++ b/mimic-iv/concepts_duckdb/firstday/first_day_sofa.sql @@ -150,7 +150,13 @@ WITH vaso_stg AS ( CASE WHEN rate_dopamine > 15 OR rate_epinephrine > 0.1 OR rate_norepinephrine > 0.1 THEN 4 - WHEN rate_dopamine > 5 OR rate_epinephrine <= 0.1 OR rate_norepinephrine <= 0.1 + WHEN rate_dopamine > 5 + OR ( + rate_epinephrine > 0 AND rate_epinephrine <= 0.1 + ) + OR ( + rate_norepinephrine > 0 AND rate_norepinephrine <= 0.1 + ) THEN 3 WHEN rate_dopamine > 0 OR rate_dobutamine > 0 THEN 2 diff --git a/mimic-iv/concepts_duckdb/measurement/urine_output_rate.sql b/mimic-iv/concepts_duckdb/measurement/urine_output_rate.sql index f39e4b41..6e93f57e 100644 --- a/mimic-iv/concepts_duckdb/measurement/urine_output_rate.sql +++ b/mimic-iv/concepts_duckdb/measurement/urine_output_rate.sql @@ -18,8 +18,8 @@ WITH tm AS ( tm.stay_id, CASE WHEN LAG(charttime) OVER w IS NULL - THEN DATE_DIFF('MINUTE', intime_hr, charttime) - ELSE DATE_DIFF('MINUTE', LAG(charttime) OVER w, charttime) + THEN DATE_DIFF('SECOND', intime_hr, charttime) / 60.0 + ELSE DATE_DIFF('SECOND', LAG(charttime) OVER w, charttime) / 60.0 END AS tm_since_last_uo, uo.charttime, uo.urineoutput @@ -34,7 +34,7 @@ WITH tm AS ( SUM(DISTINCT io.urineoutput) AS uo, SUM( CASE - WHEN DATE_DIFF('HOUR', iosum.charttime, io.charttime) <= 5 + WHEN DATE_DIFF('SECOND', iosum.charttime, io.charttime) / 3600.0 <= 5 THEN iosum.urineoutput ELSE NULL END @@ -42,7 +42,7 @@ WITH tm AS ( ROUND( CAST(SUM( CASE - WHEN DATE_DIFF('HOUR', iosum.charttime, io.charttime) <= 5 + WHEN DATE_DIFF('SECOND', iosum.charttime, io.charttime) / 3600.0 <= 5 THEN iosum.tm_since_last_uo ELSE NULL END @@ -51,7 +51,7 @@ WITH tm AS ( ) AS uo_tm_6hr, SUM( CASE - WHEN DATE_DIFF('HOUR', iosum.charttime, io.charttime) <= 11 + WHEN DATE_DIFF('SECOND', iosum.charttime, io.charttime) / 3600.0 <= 11 THEN iosum.urineoutput ELSE NULL END @@ -59,7 +59,7 @@ WITH tm AS ( ROUND( CAST(SUM( CASE - WHEN DATE_DIFF('HOUR', iosum.charttime, io.charttime) <= 11 + WHEN DATE_DIFF('SECOND', iosum.charttime, io.charttime) / 3600.0 <= 11 THEN iosum.tm_since_last_uo ELSE NULL END diff --git a/mimic-iv/concepts_duckdb/score/sofa.sql b/mimic-iv/concepts_duckdb/score/sofa.sql index aaf73837..d988d752 100644 --- a/mimic-iv/concepts_duckdb/score/sofa.sql +++ b/mimic-iv/concepts_duckdb/score/sofa.sql @@ -240,7 +240,13 @@ WITH co AS ( CASE WHEN rate_dopamine > 15 OR rate_epinephrine > 0.1 OR rate_norepinephrine > 0.1 THEN 4 - WHEN rate_dopamine > 5 OR rate_epinephrine <= 0.1 OR rate_norepinephrine <= 0.1 + WHEN rate_dopamine > 5 + OR ( + rate_epinephrine > 0 AND rate_epinephrine <= 0.1 + ) + OR ( + rate_norepinephrine > 0 AND rate_norepinephrine <= 0.1 + ) THEN 3 WHEN rate_dopamine > 0 OR rate_dobutamine > 0 THEN 2 diff --git a/mimic-iv/concepts_postgres/firstday/first_day_sofa.sql b/mimic-iv/concepts_postgres/firstday/first_day_sofa.sql index e21b7ac2..a3bd6309 100644 --- a/mimic-iv/concepts_postgres/firstday/first_day_sofa.sql +++ b/mimic-iv/concepts_postgres/firstday/first_day_sofa.sql @@ -1,6 +1,32 @@ -- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY. DROP TABLE IF EXISTS mimiciv_derived.first_day_sofa; CREATE TABLE mimiciv_derived.first_day_sofa AS -/* ------------------------------------------------------------------ */ /* Title: Sequential Organ Failure Assessment (SOFA) */ /* This query extracts the sequential organ failure assessment */ /* (formerly: sepsis-related organ failure assessment). */ /* This score is a measure of organ failure for patients in the ICU. */ /* The score is calculated on the first day of each ICU patients' stay. */ /* ------------------------------------------------------------------ */ /* Reference for SOFA: */ /* Jean-Louis Vincent, Rui Moreno, Jukka Takala, Sheila Willatts, */ /* Arnaldo De Mendonça, Hajo Bruining, C. K. Reinhart, Peter M Suter, */ /* and L. G. Thijs. */ /* "The SOFA (Sepsis-related Organ Failure Assessment) score to describe */ /* organ dysfunction/failure." */ /* Intensive care medicine 22, no. 7 (1996): 707-710. */ /* Variables used in SOFA: */ /* GCS, MAP, FiO2, Ventilation status (sourced from CHARTEVENTS) */ /* Creatinine, Bilirubin, FiO2, PaO2, Platelets (sourced from LABEVENTS) */ /* Dopamine, Dobutamine, Epinephrine, Norepinephrine (sourced from INPUTEVENTS) */ /* Urine output (sourced from OUTPUTEVENTS) */ /* The following views required to run this query: */ /* 1) first_day_urine_output */ /* 2) first_day_vitalsign */ /* 3) first_day_gcs */ /* 4) first_day_lab */ /* 5) first_day_bg_art */ /* 6) ventdurations */ /* extract drug rates from derived vasopressor tables */ +/* ------------------------------------------------------------------ */ +/* Title: Sequential Organ Failure Assessment (SOFA) */ +/* This query extracts the sequential organ failure assessment */ +/* (formerly: sepsis-related organ failure assessment). */ +/* This score is a measure of organ failure for patients in the ICU. */ +/* The score is calculated on the first day of each ICU patients' stay. */ +/* ------------------------------------------------------------------ */ +/* Reference for SOFA: */ +/* Jean-Louis Vincent, Rui Moreno, Jukka Takala, Sheila Willatts, */ +/* Arnaldo De Mendonça, Hajo Bruining, C. K. Reinhart, Peter M Suter, */ +/* and L. G. Thijs. */ +/* "The SOFA (Sepsis-related Organ Failure Assessment) score to describe */ +/* organ dysfunction/failure." */ +/* Intensive care medicine 22, no. 7 (1996): 707-710. */ +/* Variables used in SOFA: */ +/* GCS, MAP, FiO2, Ventilation status (sourced from CHARTEVENTS) */ +/* Creatinine, Bilirubin, FiO2, PaO2, Platelets (sourced from LABEVENTS) */ +/* Dopamine, Dobutamine, Epinephrine, Norepinephrine (sourced from INPUTEVENTS) */ +/* Urine output (sourced from OUTPUTEVENTS) */ +/* The following views required to run this query: */ +/* 1) first_day_urine_output */ +/* 2) first_day_vitalsign */ +/* 3) first_day_gcs */ +/* 4) first_day_lab */ +/* 5) first_day_bg_art */ +/* 6) ventdurations */ +/* extract drug rates from derived vasopressor tables */ WITH vaso_stg AS ( SELECT ie.stay_id, @@ -72,7 +98,11 @@ WITH vaso_stg AS ( AND bg.charttime <= vd.endtime AND vd.ventilation_status = 'InvasiveVent' ), pafi2 AS ( - /* because pafi has an interaction between vent/PaO2:FiO2, */ /* we need two columns for the score */ /* it can happen that the lowest unventilated PaO2/FiO2 is 68, */ /* but the lowest ventilated PaO2/FiO2 is 120 */ /* in this case, the SOFA score is 3, *not* 4. */ + /* because pafi has an interaction between vent/PaO2:FiO2, */ + /* we need two columns for the score */ + /* it can happen that the lowest unventilated PaO2/FiO2 is 68, */ + /* but the lowest ventilated PaO2/FiO2 is 120 */ + /* in this case, the SOFA score is 3, *not* 4. */ SELECT stay_id, MIN(CASE WHEN isvent = 0 THEN pao2fio2ratio ELSE NULL END) AS pao2fio2_novent_min, @@ -109,7 +139,10 @@ WITH vaso_stg AS ( LEFT JOIN mimiciv_derived.first_day_gcs AS gcs ON ie.stay_id = gcs.stay_id ), scorecalc AS ( - /* Calculate the final score */ /* note that if the underlying data is missing, the component is null */ /* eventually these are treated as 0 (normal), but knowing when data */ /* is missing is useful for debugging */ + /* Calculate the final score */ + /* note that if the underlying data is missing, the component is null */ + /* eventually these are treated as 0 (normal), but knowing when data */ + /* is missing is useful for debugging */ SELECT stay_id, /* Respiration */ CASE @@ -154,7 +187,13 @@ WITH vaso_stg AS ( CASE WHEN rate_dopamine > 15 OR rate_epinephrine > 0.1 OR rate_norepinephrine > 0.1 THEN 4 - WHEN rate_dopamine > 5 OR rate_epinephrine <= 0.1 OR rate_norepinephrine <= 0.1 + WHEN rate_dopamine > 5 + OR ( + rate_epinephrine > 0 AND rate_epinephrine <= 0.1 + ) + OR ( + rate_norepinephrine > 0 AND rate_norepinephrine <= 0.1 + ) THEN 3 WHEN rate_dopamine > 0 OR rate_dobutamine > 0 THEN 2 diff --git a/mimic-iv/concepts_postgres/measurement/urine_output_rate.sql b/mimic-iv/concepts_postgres/measurement/urine_output_rate.sql index 4cdc9ef7..b0e6e185 100644 --- a/mimic-iv/concepts_postgres/measurement/urine_output_rate.sql +++ b/mimic-iv/concepts_postgres/measurement/urine_output_rate.sql @@ -1,6 +1,9 @@ -- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY. DROP TABLE IF EXISTS mimiciv_derived.urine_output_rate; CREATE TABLE mimiciv_derived.urine_output_rate AS -/* attempt to calculate urine output per hour */ /* rate/hour is the interpretable measure of kidney function */ /* though it is difficult to estimate from aperiodic point measures */ /* first we get the earliest heart rate documented for the stay */ +/* attempt to calculate urine output per hour */ +/* rate/hour is the interpretable measure of kidney function */ +/* though it is difficult to estimate from aperiodic point measures */ +/* first we get the earliest heart rate documented for the stay */ WITH tm AS ( SELECT ie.stay_id, @@ -14,13 +17,13 @@ WITH tm AS ( AND ce.charttime < ie.outtime + INTERVAL '1' MONTH GROUP BY ie.stay_id -), uo_tm /* now calculate time since last UO measurement */ AS ( +), uo_tm /* now calculate time since last UO measurement */ /* Use SECOND then convert so BigQuery (truncating) and Postgres (fractional) */ /* DATETIME_DIFF agree; same approach as kdigo_uo.sql. */ AS ( SELECT tm.stay_id, CASE WHEN LAG(charttime) OVER w IS NULL - THEN CAST(EXTRACT(EPOCH FROM DATE_TRUNC('minute', charttime) - DATE_TRUNC('minute', intime_hr)) / 60 AS BIGINT) - ELSE CAST(EXTRACT(EPOCH FROM DATE_TRUNC('minute', charttime) - DATE_TRUNC('minute', LAG(charttime) OVER w)) / 60 AS BIGINT) + THEN CAST(CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', charttime) - DATE_TRUNC('second', intime_hr)) / 1 AS BIGINT) AS DOUBLE PRECISION) / 60.0 + ELSE CAST(CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', charttime) - DATE_TRUNC('second', LAG(charttime) OVER w)) / 1 AS BIGINT) AS DOUBLE PRECISION) / 60.0 END AS tm_since_last_uo, uo.charttime, uo.urineoutput @@ -35,7 +38,7 @@ WITH tm AS ( SUM(DISTINCT io.urineoutput) AS uo, /* note that we assume data charted at charttime corresponds */ /* to 1 hour of UO, therefore we use '5' and '11' to restrict the */ /* period, rather than 6/12 this assumption may overestimate UO rate */ /* when documentation is done less than hourly */ SUM( CASE - WHEN CAST(EXTRACT(EPOCH FROM DATE_TRUNC('hour', io.charttime) - DATE_TRUNC('hour', iosum.charttime)) / 3600 AS BIGINT) <= 5 + WHEN CAST(CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', io.charttime) - DATE_TRUNC('second', iosum.charttime)) / 1 AS BIGINT) AS DOUBLE PRECISION) / 3600.0 <= 5 THEN iosum.urineoutput ELSE NULL END @@ -43,7 +46,7 @@ WITH tm AS ( ROUND( CAST(CAST(SUM( CASE - WHEN CAST(EXTRACT(EPOCH FROM DATE_TRUNC('hour', io.charttime) - DATE_TRUNC('hour', iosum.charttime)) / 3600 AS BIGINT) <= 5 + WHEN CAST(CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', io.charttime) - DATE_TRUNC('second', iosum.charttime)) / 1 AS BIGINT) AS DOUBLE PRECISION) / 3600.0 <= 5 THEN iosum.tm_since_last_uo ELSE NULL END @@ -52,7 +55,7 @@ WITH tm AS ( ) AS uo_tm_6hr, SUM( CASE - WHEN CAST(EXTRACT(EPOCH FROM DATE_TRUNC('hour', io.charttime) - DATE_TRUNC('hour', iosum.charttime)) / 3600 AS BIGINT) <= 11 + WHEN CAST(CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', io.charttime) - DATE_TRUNC('second', iosum.charttime)) / 1 AS BIGINT) AS DOUBLE PRECISION) / 3600.0 <= 11 THEN iosum.urineoutput ELSE NULL END @@ -60,7 +63,7 @@ WITH tm AS ( ROUND( CAST(CAST(SUM( CASE - WHEN CAST(EXTRACT(EPOCH FROM DATE_TRUNC('hour', io.charttime) - DATE_TRUNC('hour', iosum.charttime)) / 3600 AS BIGINT) <= 11 + WHEN CAST(CAST(EXTRACT(EPOCH FROM DATE_TRUNC('second', io.charttime) - DATE_TRUNC('second', iosum.charttime)) / 1 AS BIGINT) AS DOUBLE PRECISION) / 3600.0 <= 11 THEN iosum.tm_since_last_uo ELSE NULL END diff --git a/mimic-iv/concepts_postgres/score/sofa.sql b/mimic-iv/concepts_postgres/score/sofa.sql index d59a5173..e81f49b0 100644 --- a/mimic-iv/concepts_postgres/score/sofa.sql +++ b/mimic-iv/concepts_postgres/score/sofa.sql @@ -1,6 +1,30 @@ -- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY. DROP TABLE IF EXISTS mimiciv_derived.sofa; CREATE TABLE mimiciv_derived.sofa AS -/* ------------------------------------------------------------------ */ /* Title: Sequential Organ Failure Assessment (SOFA) */ /* This query extracts the sequential organ failure assessment */ /* (formally: sepsis-related organ failure assessment). */ /* This score is a measure of organ failure for patients in the ICU. */ /* The score is calculated for **every hour** of the patient's ICU stay. */ /* However, as the calculation window is 24 hours, care should be */ /* taken when using the score before the end of the first day, */ /* as the data window is limited. */ /* ------------------------------------------------------------------ */ /* Reference for SOFA: */ /* Jean-Louis Vincent, Rui Moreno, Jukka Takala, Sheila Willatts, */ /* Arnaldo De Mendonça, Hajo Bruining, C. K. Reinhart, */ /* Peter M Suter, and L. G. Thijs. */ /* "The SOFA (Sepsis-related Organ Failure Assessment) score to */ /* describe organ dysfunction/failure." */ /* Intensive care medicine 22, no. 7 (1996): 707-710. */ /* Variables used in SOFA: */ /* GCS, MAP, FiO2, Ventilation status (chartevents) */ /* Creatinine, Bilirubin, FiO2, PaO2, Platelets (labevents) */ /* Dopamine, Dobutamine, Epinephrine, Norepinephrine (inputevents) */ /* Urine output (outputevents) */ /* use icustay_hourly to get a row for every hour the patient was in the ICU */ /* all of our joins to data will use these times */ /* to extract data pertinent to only that hour */ +/* ------------------------------------------------------------------ */ +/* Title: Sequential Organ Failure Assessment (SOFA) */ +/* This query extracts the sequential organ failure assessment */ +/* (formally: sepsis-related organ failure assessment). */ +/* This score is a measure of organ failure for patients in the ICU. */ +/* The score is calculated for **every hour** of the patient's ICU stay. */ +/* However, as the calculation window is 24 hours, care should be */ +/* taken when using the score before the end of the first day, */ +/* as the data window is limited. */ +/* ------------------------------------------------------------------ */ +/* Reference for SOFA: */ +/* Jean-Louis Vincent, Rui Moreno, Jukka Takala, Sheila Willatts, */ +/* Arnaldo De Mendonça, Hajo Bruining, C. K. Reinhart, */ +/* Peter M Suter, and L. G. Thijs. */ +/* "The SOFA (Sepsis-related Organ Failure Assessment) score to */ +/* describe organ dysfunction/failure." */ +/* Intensive care medicine 22, no. 7 (1996): 707-710. */ +/* Variables used in SOFA: */ +/* GCS, MAP, FiO2, Ventilation status (chartevents) */ +/* Creatinine, Bilirubin, FiO2, PaO2, Platelets (labevents) */ +/* Dopamine, Dobutamine, Epinephrine, Norepinephrine (inputevents) */ +/* Urine output (outputevents) */ +/* use icustay_hourly to get a row for every hour the patient was in the ICU */ +/* all of our joins to data will use these times */ +/* to extract data pertinent to only that hour */ WITH co AS ( SELECT ih.stay_id, @@ -195,7 +219,11 @@ WITH co AS ( LEFT JOIN vaso ON co.stay_id = vaso.stay_id AND co.hr = vaso.hr ), scorecalc AS ( - /* Calculate the final score */ /* note that if the underlying data is missing, */ /* the component is null */ /* eventually these are treated as 0 (normal), */ /* but knowing when data is missing is useful for debugging */ + /* Calculate the final score */ + /* note that if the underlying data is missing, */ + /* the component is null */ + /* eventually these are treated as 0 (normal), */ + /* but knowing when data is missing is useful for debugging */ SELECT scorecomp.*, /* Respiration */ CASE @@ -244,7 +272,13 @@ WITH co AS ( CASE WHEN rate_dopamine > 15 OR rate_epinephrine > 0.1 OR rate_norepinephrine > 0.1 THEN 4 - WHEN rate_dopamine > 5 OR rate_epinephrine <= 0.1 OR rate_norepinephrine <= 0.1 + WHEN rate_dopamine > 5 + OR ( + rate_epinephrine > 0 AND rate_epinephrine <= 0.1 + ) + OR ( + rate_norepinephrine > 0 AND rate_norepinephrine <= 0.1 + ) THEN 3 WHEN rate_dopamine > 0 OR rate_dobutamine > 0 THEN 2