-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathfirst_day_gcs.sql
More file actions
54 lines (51 loc) · 1.81 KB
/
Copy pathfirst_day_gcs.sql
File metadata and controls
54 lines (51 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- Glasgow Coma Scale, a measure of neurological function.
-- Ranges from 3 (worst, comatose) to 15 (best, normal function).
-- Note:
-- The GCS for sedated patients is defaulted to 15 in this code.
-- This follows common practice for scoring patients with severity
-- of illness scores.
--
-- e.g., from the SAPS II publication:
-- For sedated patients, the Glasgow Coma Score before sedation was used.
-- This was ascertained either from interviewing the physician who ordered
-- the sedation, or by reviewing the patient's medical record.
WITH gcs_final AS (
SELECT
ie.subject_id, ie.stay_id
, g.gcs
, g.gcs_motor
, g.gcs_verbal
, g.gcs_eyes
, g.gcs_unable
-- This sorts the data by GCS
-- rn = 1 is the the lowest total GCS value
-- tie-break on charttime (unique per stay) so the component columns
-- are deterministic across SQL engines when multiple measurements
-- share the same minimum GCS
, ROW_NUMBER() OVER
(
PARTITION BY g.stay_id
ORDER BY g.gcs ASC NULLS LAST, g.charttime DESC NULLS LAST
) AS gcs_seq
FROM `physionet-data.mimiciv_icu.icustays` ie
-- Only get data for the first 24 hours
LEFT JOIN `physionet-data.mimiciv_derived.gcs` g
ON ie.stay_id = g.stay_id
AND g.charttime >= DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)
AND g.charttime <= DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
)
SELECT
ie.subject_id
, ie.stay_id
-- The minimum GCS is determined by the above row partition
-- we only join if gcs_seq = 1
, gcs AS gcs_min
, gcs_motor
, gcs_verbal
, gcs_eyes
, gcs_unable
FROM `physionet-data.mimiciv_icu.icustays` ie
LEFT JOIN gcs_final gs
ON ie.stay_id = gs.stay_id
AND gs.gcs_seq = 1
;