You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: mimic-iv/concepts_postgres/sepsis/suspicion_of_infection.sql
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -9,10 +9,10 @@ WITH ab_tbl AS (
9
9
abx.antibiotic,
10
10
abx.starttimeAS antibiotic_time, /* date is used to match microbiology cultures with only date available */
11
11
DATE_TRUNC('day', abx.starttime) AS antibiotic_date,
12
-
abx.stoptimeAS antibiotic_stoptime, /* create a unique identifier for each patient antibiotic */
12
+
abx.stoptimeAS antibiotic_stoptime, /* create a unique identifier for each patient antibiotic *//* hadm_id/stay_id tiebreak keeps the numbering deterministic when *//* multiple antibiotics share the same start/stop time and name */
13
13
ROW_NUMBER() OVER (
14
14
PARTITION BY subject_id
15
-
ORDER BY starttime NULLS FIRST, stoptime NULLS FIRST, antibiotic NULLS FIRST
15
+
ORDER BY starttime NULLS FIRST, stoptime NULLS FIRST, antibiotic NULLS FIRST, hadm_id NULLS FIRST, stay_id NULLS FIRST
16
16
) AS ab_id
17
17
FROMmimiciv_derived.antibioticAS abx
18
18
), me AS (
@@ -42,10 +42,10 @@ WITH ab_tbl AS (
42
42
me72.micro_specimen_id,
43
43
COALESCE(me72.charttime, CAST(me72.chartdateASTIMESTAMP)) AS last72_charttime,
44
44
me72.positivecultureAS last72_positiveculture,
45
-
me72.spec_type_descAS last72_specimen, /* we will use this partition to select the earliest culture *//* before this abx *//* this ensures each antibiotic is only matched to a single culture *//* and consequently we have 1 row per antibiotic */
45
+
me72.spec_type_descAS last72_specimen, /* we will use this partition to select the earliest culture *//* before this abx *//* this ensures each antibiotic is only matched to a single culture *//* and consequently we have 1 row per antibiotic *//* among cultures at the same time point, prefer a positive culture *//* (positiveculture DESC) so that any positive culture yields *//* positive_culture = 1, per the suspicion of infection criteria; the *//* micro_specimen_id tiebreak makes the selection deterministic */
46
46
ROW_NUMBER() OVER (
47
47
PARTITION BY ab_tbl.subject_id, ab_tbl.ab_id
48
-
ORDER BYme72.chartdate NULLS FIRST, me72.charttime
48
+
ORDER BYme72.chartdate NULLS FIRST, me72.charttime, me72.positivecultureDESC NULLS LAST, me72.micro_specimen_id NULLS FIRST
49
49
) AS micro_seq
50
50
FROM ab_tbl
51
51
/* abx taken after culture, but no more than 72 hours after */
@@ -72,10 +72,10 @@ WITH ab_tbl AS (
72
72
me24.micro_specimen_id,
73
73
COALESCE(me24.charttime, CAST(me24.chartdateASTIMESTAMP)) AS next24_charttime,
74
74
me24.positivecultureAS next24_positiveculture,
75
-
me24.spec_type_descAS next24_specimen, /* we will use this partition to select the earliest culture *//* before this abx *//* this ensures each antibiotic is only matched to a single culture *//* and consequently we have 1 row per antibiotic */
75
+
me24.spec_type_descAS next24_specimen, /* we will use this partition to select the earliest culture *//* before this abx *//* this ensures each antibiotic is only matched to a single culture *//* and consequently we have 1 row per antibiotic *//* among cultures at the same time point, prefer a positive culture *//* (positiveculture DESC) so that any positive culture yields *//* positive_culture = 1, per the suspicion of infection criteria; the *//* micro_specimen_id tiebreak makes the selection deterministic */
76
76
ROW_NUMBER() OVER (
77
77
PARTITION BY ab_tbl.subject_id, ab_tbl.ab_id
78
-
ORDER BYme24.chartdate NULLS FIRST, me24.charttime
78
+
ORDER BYme24.chartdate NULLS FIRST, me24.charttime, me24.positivecultureDESC NULLS LAST, me24.micro_specimen_id NULLS FIRST
0 commit comments