initial
This commit is contained in:
108
sql/firstday/blood_gas_first_day.sql
Normal file
108
sql/firstday/blood_gas_first_day.sql
Normal file
@@ -0,0 +1,108 @@
|
||||
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
|
||||
DROP TABLE IF EXISTS blood_gas_first_day; CREATE TABLE blood_gas_first_day AS
|
||||
-- The aim of this query is to pivot entries related to blood gases and
|
||||
-- chemistry values which were found in LABEVENTS
|
||||
|
||||
-- things to check:
|
||||
-- when a mixed venous/arterial blood sample are taken at the same time, is the store time different?
|
||||
|
||||
with pvt as
|
||||
( -- begin query that extracts the data
|
||||
select ie.subject_id, ie.hadm_id, ie.icustay_id
|
||||
-- here we assign labels to ITEMIDs
|
||||
-- this also fuses together multiple ITEMIDs containing the same data
|
||||
, case
|
||||
when itemid = 50800 then 'SPECIMEN'
|
||||
when itemid = 50801 then 'AADO2'
|
||||
when itemid = 50802 then 'BASEEXCESS'
|
||||
when itemid = 50803 then 'BICARBONATE'
|
||||
when itemid = 50804 then 'TOTALCO2'
|
||||
when itemid = 50805 then 'CARBOXYHEMOGLOBIN'
|
||||
when itemid = 50806 then 'CHLORIDE'
|
||||
when itemid = 50808 then 'CALCIUM'
|
||||
when itemid = 50809 then 'GLUCOSE'
|
||||
when itemid = 50810 then 'HEMATOCRIT'
|
||||
when itemid = 50811 then 'HEMOGLOBIN'
|
||||
when itemid = 50812 then 'INTUBATED'
|
||||
when itemid = 50813 then 'LACTATE'
|
||||
when itemid = 50814 then 'METHEMOGLOBIN'
|
||||
when itemid = 50815 then 'O2FLOW'
|
||||
when itemid = 50816 then 'FIO2'
|
||||
when itemid = 50817 then 'SO2' -- OXYGENSATURATION
|
||||
when itemid = 50818 then 'PCO2'
|
||||
when itemid = 50819 then 'PEEP'
|
||||
when itemid = 50820 then 'PH'
|
||||
when itemid = 50821 then 'PO2'
|
||||
when itemid = 50822 then 'POTASSIUM'
|
||||
when itemid = 50823 then 'REQUIREDO2'
|
||||
when itemid = 50824 then 'SODIUM'
|
||||
when itemid = 50825 then 'TEMPERATURE'
|
||||
when itemid = 50826 then 'TIDALVOLUME'
|
||||
when itemid = 50827 then 'VENTILATIONRATE'
|
||||
when itemid = 50828 then 'VENTILATOR'
|
||||
else null
|
||||
end as label
|
||||
, charttime
|
||||
, value
|
||||
-- add in some sanity checks on the values
|
||||
, case
|
||||
when valuenum <= 0 and itemid != 50802 then null -- allow negative baseexcess
|
||||
when itemid = 50810 and valuenum > 100 then null -- hematocrit
|
||||
-- ensure FiO2 is a valid number between 21-100
|
||||
-- mistakes are rare (<100 obs out of ~100,000)
|
||||
-- there are 862 obs of valuenum == 20 - some people round down!
|
||||
-- rather than risk imputing garbage data for FiO2, we simply NULL invalid values
|
||||
when itemid = 50816 and valuenum < 20 then null
|
||||
when itemid = 50816 and valuenum > 100 then null
|
||||
when itemid = 50817 and valuenum > 100 then null -- O2 sat
|
||||
when itemid = 50815 and valuenum > 70 then null -- O2 flow
|
||||
when itemid = 50821 and valuenum > 800 then null -- PO2
|
||||
-- conservative upper limit
|
||||
else valuenum
|
||||
end as valuenum
|
||||
|
||||
FROM icustays ie
|
||||
left join labevents le
|
||||
on le.subject_id = ie.subject_id and le.hadm_id = ie.hadm_id
|
||||
and le.charttime between (DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)) and (DATETIME_ADD(ie.intime, INTERVAL '1' DAY))
|
||||
and le.ITEMID in
|
||||
-- blood gases
|
||||
(
|
||||
50800, 50801, 50802, 50803, 50804, 50805, 50806, 50807, 50808, 50809
|
||||
, 50810, 50811, 50812, 50813, 50814, 50815, 50816, 50817, 50818, 50819
|
||||
, 50820, 50821, 50822, 50823, 50824, 50825, 50826, 50827, 50828
|
||||
, 51545
|
||||
)
|
||||
)
|
||||
select pvt.SUBJECT_ID, pvt.HADM_ID, pvt.ICUSTAY_ID, pvt.CHARTTIME
|
||||
, max(case when label = 'SPECIMEN' then value else null end) as specimen
|
||||
, max(case when label = 'AADO2' then valuenum else null end) as aado2
|
||||
, max(case when label = 'BASEEXCESS' then valuenum else null end) as baseexcess
|
||||
, max(case when label = 'BICARBONATE' then valuenum else null end) as bicarbonate
|
||||
, max(case when label = 'TOTALCO2' then valuenum else null end) as totalco2
|
||||
, max(case when label = 'CARBOXYHEMOGLOBIN' then valuenum else null end) as carboxyhemoglobin
|
||||
, max(case when label = 'CHLORIDE' then valuenum else null end) as chloride
|
||||
, max(case when label = 'CALCIUM' then valuenum else null end) as calcium
|
||||
, max(case when label = 'GLUCOSE' then valuenum else null end) as glucose
|
||||
, max(case when label = 'HEMATOCRIT' then valuenum else null end) as hematocrit
|
||||
, max(case when label = 'HEMOGLOBIN' then valuenum else null end) as hemoglobin
|
||||
, max(case when label = 'INTUBATED' then valuenum else null end) as intubated
|
||||
, max(case when label = 'LACTATE' then valuenum else null end) as lactate
|
||||
, max(case when label = 'METHEMOGLOBIN' then valuenum else null end) as methemoglobin
|
||||
, max(case when label = 'O2FLOW' then valuenum else null end) as o2flow
|
||||
, max(case when label = 'FIO2' then valuenum else null end) as fio2
|
||||
, max(case when label = 'SO2' then valuenum else null end) as so2 -- OXYGENSATURATION
|
||||
, max(case when label = 'PCO2' then valuenum else null end) as pco2
|
||||
, max(case when label = 'PEEP' then valuenum else null end) as peep
|
||||
, max(case when label = 'PH' then valuenum else null end) as ph
|
||||
, max(case when label = 'PO2' then valuenum else null end) as po2
|
||||
, max(case when label = 'POTASSIUM' then valuenum else null end) as potassium
|
||||
, max(case when label = 'REQUIREDO2' then valuenum else null end) as requiredo2
|
||||
, max(case when label = 'SODIUM' then valuenum else null end) as sodium
|
||||
, max(case when label = 'TEMPERATURE' then valuenum else null end) as temperature
|
||||
, max(case when label = 'TIDALVOLUME' then valuenum else null end) as tidalvolume
|
||||
, max(case when label = 'VENTILATIONRATE' then valuenum else null end) as ventilationrate
|
||||
, max(case when label = 'VENTILATOR' then valuenum else null end) as ventilator
|
||||
from pvt
|
||||
group by pvt.subject_id, pvt.hadm_id, pvt.icustay_id, pvt.CHARTTIME
|
||||
order by pvt.subject_id, pvt.hadm_id, pvt.icustay_id, pvt.CHARTTIME;
|
||||
156
sql/firstday/blood_gas_first_day_arterial.sql
Normal file
156
sql/firstday/blood_gas_first_day_arterial.sql
Normal file
@@ -0,0 +1,156 @@
|
||||
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
|
||||
DROP TABLE IF EXISTS blood_gas_first_day_arterial; CREATE TABLE blood_gas_first_day_arterial AS
|
||||
|
||||
with stg_spo2 as
|
||||
(
|
||||
select subject_id, hadm_id, icustay_id, charttime
|
||||
-- max here is just used to group SpO2 by charttime
|
||||
, max(case when valuenum <= 0 or valuenum > 100 then null else valuenum end) as SpO2
|
||||
FROM chartevents
|
||||
-- o2 sat
|
||||
where ITEMID in
|
||||
(
|
||||
646 -- SpO2
|
||||
, 220277 -- O2 saturation pulseoxymetry
|
||||
)
|
||||
group by subject_id, hadm_id, icustay_id, charttime
|
||||
)
|
||||
, stg_fio2 as
|
||||
(
|
||||
select subject_id, hadm_id, icustay_id, charttime
|
||||
-- pre-process the FiO2s to ensure they are between 21-100%
|
||||
, max(
|
||||
case
|
||||
when itemid = 223835
|
||||
then case
|
||||
when valuenum > 0 and valuenum <= 1
|
||||
then valuenum * 100
|
||||
-- improperly input data - looks like O2 flow in litres
|
||||
when valuenum > 1 and valuenum < 21
|
||||
then null
|
||||
when valuenum >= 21 and valuenum <= 100
|
||||
then valuenum
|
||||
else null end -- unphysiological
|
||||
when itemid in (3420, 3422)
|
||||
-- all these values are well formatted
|
||||
then valuenum
|
||||
when itemid = 190 and valuenum > 0.20 and valuenum < 1
|
||||
-- well formatted but not in %
|
||||
then valuenum * 100
|
||||
else null end
|
||||
) as fio2_chartevents
|
||||
FROM chartevents
|
||||
where ITEMID in
|
||||
(
|
||||
3420 -- FiO2
|
||||
, 190 -- FiO2 set
|
||||
, 223835 -- Inspired O2 Fraction (FiO2)
|
||||
, 3422 -- FiO2 [measured]
|
||||
)
|
||||
-- exclude rows marked as error
|
||||
AND (error IS NULL OR error = 0)
|
||||
group by subject_id, hadm_id, icustay_id, charttime
|
||||
)
|
||||
, stg2 as
|
||||
(
|
||||
select bg.*
|
||||
, ROW_NUMBER() OVER (partition by bg.icustay_id, bg.charttime order by s1.charttime DESC) as lastRowSpO2
|
||||
, s1.spo2
|
||||
from blood_gas_first_day bg
|
||||
left join stg_spo2 s1
|
||||
-- same patient
|
||||
on bg.icustay_id = s1.icustay_id
|
||||
-- spo2 occurred at most 2 hours before this blood gas
|
||||
and s1.charttime >= DATETIME_SUB(bg.charttime, INTERVAL '2' HOUR)
|
||||
and s1.charttime <= bg.charttime
|
||||
where bg.po2 is not null
|
||||
)
|
||||
, stg3 as
|
||||
(
|
||||
select bg.*
|
||||
, ROW_NUMBER() OVER (partition by bg.icustay_id, bg.charttime order by s2.charttime DESC) as lastRowFiO2
|
||||
, s2.fio2_chartevents
|
||||
|
||||
-- create our specimen prediction
|
||||
, 1/(1+exp(-(-0.02544
|
||||
+ 0.04598 * po2
|
||||
+ coalesce(-0.15356 * spo2 , -0.15356 * 97.49420 + 0.13429)
|
||||
+ coalesce( 0.00621 * fio2_chartevents , 0.00621 * 51.49550 + -0.24958)
|
||||
+ coalesce( 0.10559 * hemoglobin , 0.10559 * 10.32307 + 0.05954)
|
||||
+ coalesce( 0.13251 * so2 , 0.13251 * 93.66539 + -0.23172)
|
||||
+ coalesce(-0.01511 * pco2 , -0.01511 * 42.08866 + -0.01630)
|
||||
+ coalesce( 0.01480 * fio2 , 0.01480 * 63.97836 + -0.31142)
|
||||
+ coalesce(-0.00200 * aado2 , -0.00200 * 442.21186 + -0.01328)
|
||||
+ coalesce(-0.03220 * bicarbonate , -0.03220 * 22.96894 + -0.06535)
|
||||
+ coalesce( 0.05384 * totalco2 , 0.05384 * 24.72632 + -0.01405)
|
||||
+ coalesce( 0.08202 * lactate , 0.08202 * 3.06436 + 0.06038)
|
||||
+ coalesce( 0.10956 * ph , 0.10956 * 7.36233 + -0.00617)
|
||||
+ coalesce( 0.00848 * o2flow , 0.00848 * 7.59362 + -0.35803)
|
||||
))) as SPECIMEN_PROB
|
||||
from stg2 bg
|
||||
left join stg_fio2 s2
|
||||
-- same patient
|
||||
on bg.icustay_id = s2.icustay_id
|
||||
-- fio2 occurred at most 4 hours before this blood gas
|
||||
and s2.charttime between DATETIME_SUB(bg.charttime, INTERVAL '4' HOUR) and bg.charttime
|
||||
where bg.lastRowSpO2 = 1 -- only the row with the most recent SpO2 (if no SpO2 found lastRowSpO2 = 1)
|
||||
)
|
||||
|
||||
select subject_id, hadm_id,
|
||||
icustay_id, charttime
|
||||
, specimen -- raw data indicating sample type, only present 80% of the time
|
||||
|
||||
-- prediction of specimen for missing data
|
||||
, case
|
||||
when SPECIMEN is not null then SPECIMEN
|
||||
when SPECIMEN_PROB > 0.75 then 'ART'
|
||||
else null end as SPECIMEN_PRED
|
||||
, specimen_prob
|
||||
|
||||
-- oxygen related parameters
|
||||
, so2, spo2 -- note spo2 is FROM chartevents
|
||||
, po2, pco2
|
||||
, fio2_chartevents, fio2
|
||||
, aado2
|
||||
-- also calculate AADO2
|
||||
, case
|
||||
when PO2 is not null
|
||||
and pco2 is not null
|
||||
and coalesce(fio2, fio2_chartevents) is not null
|
||||
-- multiple by 100 because FiO2 is in a % but should be a fraction
|
||||
then (coalesce(fio2, fio2_chartevents)/100) * (760 - 47) - (pco2/0.8) - po2
|
||||
else null
|
||||
end as AADO2_calc
|
||||
, case
|
||||
when PO2 is not null and coalesce(fio2, fio2_chartevents) is not null
|
||||
-- multiply by 100 because FiO2 is in a % but should be a fraction
|
||||
then 100*PO2/(coalesce(fio2, fio2_chartevents))
|
||||
else null
|
||||
end as PaO2FiO2
|
||||
-- acid-base parameters
|
||||
, ph, baseexcess
|
||||
, bicarbonate, totalco2
|
||||
|
||||
-- blood count parameters
|
||||
, hematocrit
|
||||
, hemoglobin
|
||||
, carboxyhemoglobin
|
||||
, methemoglobin
|
||||
|
||||
-- chemistry
|
||||
, chloride, calcium
|
||||
, temperature
|
||||
, potassium, sodium
|
||||
, lactate
|
||||
, glucose
|
||||
|
||||
-- ventilation stuff that's sometimes input
|
||||
, intubated, tidalvolume, ventilationrate, ventilator
|
||||
, peep, o2flow
|
||||
, requiredo2
|
||||
|
||||
from stg3
|
||||
where lastRowFiO2 = 1 -- only the most recent FiO2
|
||||
-- restrict it to *only* arterial samples
|
||||
and (specimen = 'ART' or specimen_prob > 0.75)
|
||||
order by icustay_id, charttime;
|
||||
143
sql/firstday/gcs_first_day.sql
Normal file
143
sql/firstday/gcs_first_day.sql
Normal file
@@ -0,0 +1,143 @@
|
||||
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
|
||||
DROP TABLE IF EXISTS gcs_first_day; CREATE TABLE gcs_first_day AS
|
||||
-- ITEMIDs used:
|
||||
|
||||
-- CAREVUE
|
||||
-- 723 as GCSVerbal
|
||||
-- 454 as GCSMotor
|
||||
-- 184 as GCSEyes
|
||||
|
||||
-- METAVISION
|
||||
-- 223900 GCS - Verbal Response
|
||||
-- 223901 GCS - Motor Response
|
||||
-- 220739 GCS - Eye Opening
|
||||
|
||||
-- The code combines the ITEMIDs into the carevue itemids, then pivots those
|
||||
-- So 223900 is changed to 723, then the ITEMID 723 is pivoted to form GCSVerbal
|
||||
|
||||
-- Note:
|
||||
-- The GCS for sedated patients is defaulted to 15 in this code.
|
||||
-- This is in line with how the data is meant to be collected.
|
||||
-- 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 base as
|
||||
(
|
||||
SELECT pvt.ICUSTAY_ID
|
||||
, pvt.charttime
|
||||
|
||||
-- Easier names - note we coalesced Metavision and CareVue IDs below
|
||||
, max(case when pvt.itemid = 454 then pvt.valuenum else null end) as GCSMotor
|
||||
, max(case when pvt.itemid = 723 then pvt.valuenum else null end) as GCSVerbal
|
||||
, max(case when pvt.itemid = 184 then pvt.valuenum else null end) as GCSEyes
|
||||
|
||||
-- If verbal was set to 0 in the below select, then this is an intubated patient
|
||||
, case
|
||||
when max(case when pvt.itemid = 723 then pvt.valuenum else null end) = 0
|
||||
then 1
|
||||
else 0
|
||||
end as EndoTrachFlag
|
||||
|
||||
, ROW_NUMBER ()
|
||||
OVER (PARTITION BY pvt.ICUSTAY_ID ORDER BY pvt.charttime ASC) as rn
|
||||
|
||||
FROM (
|
||||
select l.ICUSTAY_ID
|
||||
-- merge the ITEMIDs so that the pivot applies to both metavision/carevue data
|
||||
, case
|
||||
when l.ITEMID in (723,223900) then 723
|
||||
when l.ITEMID in (454,223901) then 454
|
||||
when l.ITEMID in (184,220739) then 184
|
||||
else l.ITEMID end
|
||||
as ITEMID
|
||||
|
||||
-- convert the data into a number, reserving a value of 0 for ET/Trach
|
||||
, case
|
||||
-- endotrach/vent is assigned a value of 0, later parsed specially
|
||||
when l.ITEMID = 723 and l.VALUE = '1.0 ET/Trach' then 0 -- carevue
|
||||
when l.ITEMID = 223900 and l.VALUE = 'No Response-ETT' then 0 -- metavision
|
||||
|
||||
else VALUENUM
|
||||
end
|
||||
as VALUENUM
|
||||
, l.CHARTTIME
|
||||
FROM chartevents l
|
||||
|
||||
-- get intime for charttime subselection
|
||||
inner join icustays b
|
||||
on l.icustay_id = b.icustay_id
|
||||
|
||||
-- Isolate the desired GCS variables
|
||||
where l.ITEMID in
|
||||
(
|
||||
-- 198 -- GCS
|
||||
-- GCS components, CareVue
|
||||
184, 454, 723
|
||||
-- GCS components, Metavision
|
||||
, 223900, 223901, 220739
|
||||
)
|
||||
-- Only get data for the first 24 hours
|
||||
and l.charttime between b.intime and DATETIME_ADD(b.intime, INTERVAL '1' DAY)
|
||||
-- exclude rows marked as error
|
||||
AND (l.error IS NULL OR l.error = 0)
|
||||
) pvt
|
||||
group by pvt.ICUSTAY_ID, pvt.charttime
|
||||
)
|
||||
, gcs as (
|
||||
select b.*
|
||||
, b2.GCSVerbal as GCSVerbalPrev
|
||||
, b2.GCSMotor as GCSMotorPrev
|
||||
, b2.GCSEyes as GCSEyesPrev
|
||||
-- Calculate GCS, factoring in special case when they are intubated and prev vals
|
||||
-- note that the coalesce are used to implement the following if:
|
||||
-- if current value exists, use it
|
||||
-- if previous value exists, use it
|
||||
-- otherwise, default to normal
|
||||
, case
|
||||
-- replace GCS during sedation with 15
|
||||
when b.GCSVerbal = 0
|
||||
then 15
|
||||
when b.GCSVerbal is null and b2.GCSVerbal = 0
|
||||
then 15
|
||||
-- if previously they were intub, but they aren't now, do not use previous GCS values
|
||||
when b2.GCSVerbal = 0
|
||||
then
|
||||
coalesce(b.GCSMotor,6)
|
||||
+ coalesce(b.GCSVerbal,5)
|
||||
+ coalesce(b.GCSEyes,4)
|
||||
-- otherwise, add up score normally, imputing previous value if none available at current time
|
||||
else
|
||||
coalesce(b.GCSMotor,coalesce(b2.GCSMotor,6))
|
||||
+ coalesce(b.GCSVerbal,coalesce(b2.GCSVerbal,5))
|
||||
+ coalesce(b.GCSEyes,coalesce(b2.GCSEyes,4))
|
||||
end as GCS
|
||||
|
||||
from base b
|
||||
-- join to itself within 6 hours to get previous value
|
||||
left join base b2
|
||||
on b.ICUSTAY_ID = b2.ICUSTAY_ID and b.rn = b2.rn+1 and b2.charttime > DATETIME_SUB(b.charttime, INTERVAL '6' HOUR)
|
||||
)
|
||||
, gcs_final as (
|
||||
select gcs.*
|
||||
-- This sorts the data by GCS, so rn=1 is the the lowest GCS values to keep
|
||||
, ROW_NUMBER ()
|
||||
OVER (PARTITION BY gcs.ICUSTAY_ID
|
||||
ORDER BY gcs.GCS
|
||||
) as IsMinGCS
|
||||
from gcs
|
||||
)
|
||||
select ie.subject_id, ie.hadm_id, ie.icustay_id
|
||||
-- The minimum GCS is determined by the above row partition, we only join if IsMinGCS=1
|
||||
, GCS as mingcs
|
||||
, coalesce(GCSMotor,GCSMotorPrev) as gcsmotor
|
||||
, coalesce(GCSVerbal,GCSVerbalPrev) as gcsverbal
|
||||
, coalesce(GCSEyes,GCSEyesPrev) as gcseyes
|
||||
, EndoTrachFlag as endotrachflag
|
||||
|
||||
-- subselect down to the cohort of eligible patients
|
||||
FROM icustays ie
|
||||
left join gcs_final gs
|
||||
on ie.icustay_id = gs.icustay_id and gs.IsMinGCS = 1
|
||||
ORDER BY ie.icustay_id;
|
||||
155
sql/firstday/labs_first_day.sql
Normal file
155
sql/firstday/labs_first_day.sql
Normal file
@@ -0,0 +1,155 @@
|
||||
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
|
||||
DROP TABLE IF EXISTS labs_first_day; CREATE TABLE labs_first_day AS
|
||||
-- This query pivots lab values taken in the first 24 hours of a patient's stay
|
||||
|
||||
-- Have already confirmed that the unit of measurement is always the same: null or the correct unit
|
||||
|
||||
SELECT
|
||||
pvt.subject_id, pvt.hadm_id, pvt.icustay_id
|
||||
|
||||
, min(CASE WHEN label = 'ANION GAP' THEN valuenum ELSE NULL END) AS aniongap_min
|
||||
, max(CASE WHEN label = 'ANION GAP' THEN valuenum ELSE NULL END) AS aniongap_max
|
||||
, min(CASE WHEN label = 'ALBUMIN' THEN valuenum ELSE NULL END) AS albumin_min
|
||||
, max(CASE WHEN label = 'ALBUMIN' THEN valuenum ELSE NULL END) AS albumin_max
|
||||
, min(CASE WHEN label = 'BANDS' THEN valuenum ELSE NULL END) AS bands_min
|
||||
, max(CASE WHEN label = 'BANDS' THEN valuenum ELSE NULL END) AS bands_max
|
||||
, min(CASE WHEN label = 'BICARBONATE' THEN valuenum ELSE NULL END) AS bicarbonate_min
|
||||
, max(CASE WHEN label = 'BICARBONATE' THEN valuenum ELSE NULL END) AS bicarbonate_max
|
||||
, min(CASE WHEN label = 'BILIRUBIN' THEN valuenum ELSE NULL END) AS bilirubin_min
|
||||
, max(CASE WHEN label = 'BILIRUBIN' THEN valuenum ELSE NULL END) AS bilirubin_max
|
||||
, min(CASE WHEN label = 'CREATININE' THEN valuenum ELSE NULL END) AS creatinine_min
|
||||
, max(CASE WHEN label = 'CREATININE' THEN valuenum ELSE NULL END) AS creatinine_max
|
||||
, min(CASE WHEN label = 'CHLORIDE' THEN valuenum ELSE NULL END) AS chloride_min
|
||||
, max(CASE WHEN label = 'CHLORIDE' THEN valuenum ELSE NULL END) AS chloride_max
|
||||
, min(CASE WHEN label = 'GLUCOSE' THEN valuenum ELSE NULL END) AS glucose_min
|
||||
, max(CASE WHEN label = 'GLUCOSE' THEN valuenum ELSE NULL END) AS glucose_max
|
||||
, min(CASE WHEN label = 'HEMATOCRIT' THEN valuenum ELSE NULL END) AS hematocrit_min
|
||||
, max(CASE WHEN label = 'HEMATOCRIT' THEN valuenum ELSE NULL END) AS hematocrit_max
|
||||
, min(CASE WHEN label = 'HEMOGLOBIN' THEN valuenum ELSE NULL END) AS hemoglobin_min
|
||||
, max(CASE WHEN label = 'HEMOGLOBIN' THEN valuenum ELSE NULL END) AS hemoglobin_max
|
||||
, min(CASE WHEN label = 'LACTATE' THEN valuenum ELSE NULL END) AS lactate_min
|
||||
, max(CASE WHEN label = 'LACTATE' THEN valuenum ELSE NULL END) AS lactate_max
|
||||
, min(CASE WHEN label = 'PLATELET' THEN valuenum ELSE NULL END) AS platelet_min
|
||||
, max(CASE WHEN label = 'PLATELET' THEN valuenum ELSE NULL END) AS platelet_max
|
||||
, min(CASE WHEN label = 'POTASSIUM' THEN valuenum ELSE NULL END) AS potassium_min
|
||||
, max(CASE WHEN label = 'POTASSIUM' THEN valuenum ELSE NULL END) AS potassium_max
|
||||
, min(CASE WHEN label = 'PTT' THEN valuenum ELSE NULL END) AS ptt_min
|
||||
, max(CASE WHEN label = 'PTT' THEN valuenum ELSE NULL END) AS ptt_max
|
||||
, min(CASE WHEN label = 'INR' THEN valuenum ELSE NULL END) AS inr_min
|
||||
, max(CASE WHEN label = 'INR' THEN valuenum ELSE NULL END) AS inr_max
|
||||
, min(CASE WHEN label = 'PT' THEN valuenum ELSE NULL END) AS pt_min
|
||||
, max(CASE WHEN label = 'PT' THEN valuenum ELSE NULL END) AS pt_max
|
||||
, min(CASE WHEN label = 'SODIUM' THEN valuenum ELSE NULL END) AS sodium_min
|
||||
, max(CASE WHEN label = 'SODIUM' THEN valuenum ELSE NULL END) AS sodium_max
|
||||
, min(CASE WHEN label = 'BUN' THEN valuenum ELSE NULL END) AS bun_min
|
||||
, max(CASE WHEN label = 'BUN' THEN valuenum ELSE NULL END) AS bun_max
|
||||
, min(CASE WHEN label = 'WBC' THEN valuenum ELSE NULL END) AS wbc_min
|
||||
, max(CASE WHEN label = 'WBC' THEN valuenum ELSE NULL END) AS wbc_max
|
||||
|
||||
|
||||
FROM
|
||||
( -- begin query that extracts the data
|
||||
SELECT ie.subject_id, ie.hadm_id, ie.icustay_id
|
||||
-- here we assign labels to ITEMIDs
|
||||
-- this also fuses together multiple ITEMIDs containing the same data
|
||||
, CASE
|
||||
WHEN itemid = 50868 THEN 'ANION GAP'
|
||||
WHEN itemid = 50862 THEN 'ALBUMIN'
|
||||
WHEN itemid = 51144 THEN 'BANDS'
|
||||
WHEN itemid = 50882 THEN 'BICARBONATE'
|
||||
WHEN itemid = 50885 THEN 'BILIRUBIN'
|
||||
WHEN itemid = 50912 THEN 'CREATININE'
|
||||
WHEN itemid = 50806 THEN 'CHLORIDE'
|
||||
WHEN itemid = 50902 THEN 'CHLORIDE'
|
||||
WHEN itemid = 50809 THEN 'GLUCOSE'
|
||||
WHEN itemid = 50931 THEN 'GLUCOSE'
|
||||
WHEN itemid = 50810 THEN 'HEMATOCRIT'
|
||||
WHEN itemid = 51221 THEN 'HEMATOCRIT'
|
||||
WHEN itemid = 50811 THEN 'HEMOGLOBIN'
|
||||
WHEN itemid = 51222 THEN 'HEMOGLOBIN'
|
||||
WHEN itemid = 50813 THEN 'LACTATE'
|
||||
WHEN itemid = 51265 THEN 'PLATELET'
|
||||
WHEN itemid = 50822 THEN 'POTASSIUM'
|
||||
WHEN itemid = 50971 THEN 'POTASSIUM'
|
||||
WHEN itemid = 51275 THEN 'PTT'
|
||||
WHEN itemid = 51237 THEN 'INR'
|
||||
WHEN itemid = 51274 THEN 'PT'
|
||||
WHEN itemid = 50824 THEN 'SODIUM'
|
||||
WHEN itemid = 50983 THEN 'SODIUM'
|
||||
WHEN itemid = 51006 THEN 'BUN'
|
||||
WHEN itemid = 51300 THEN 'WBC'
|
||||
WHEN itemid = 51301 THEN 'WBC'
|
||||
ELSE null
|
||||
END as label
|
||||
, -- add in some sanity checks on the values
|
||||
-- the where clause below requires all valuenum to be > 0, so these are only upper limit checks
|
||||
CASE
|
||||
WHEN itemid = 50862 and valuenum > 10 THEN null -- g/dL 'ALBUMIN'
|
||||
WHEN itemid = 50868 and valuenum > 10000 THEN null -- mEq/L 'ANION GAP'
|
||||
WHEN itemid = 51144 and valuenum < 0 THEN null -- immature band forms, %
|
||||
WHEN itemid = 51144 and valuenum > 100 THEN null -- immature band forms, %
|
||||
WHEN itemid = 50882 and valuenum > 10000 THEN null -- mEq/L 'BICARBONATE'
|
||||
WHEN itemid = 50885 and valuenum > 150 THEN null -- mg/dL 'BILIRUBIN'
|
||||
WHEN itemid = 50806 and valuenum > 10000 THEN null -- mEq/L 'CHLORIDE'
|
||||
WHEN itemid = 50902 and valuenum > 10000 THEN null -- mEq/L 'CHLORIDE'
|
||||
WHEN itemid = 50912 and valuenum > 150 THEN null -- mg/dL 'CREATININE'
|
||||
WHEN itemid = 50809 and valuenum > 10000 THEN null -- mg/dL 'GLUCOSE'
|
||||
WHEN itemid = 50931 and valuenum > 10000 THEN null -- mg/dL 'GLUCOSE'
|
||||
WHEN itemid = 50810 and valuenum > 100 THEN null -- % 'HEMATOCRIT'
|
||||
WHEN itemid = 51221 and valuenum > 100 THEN null -- % 'HEMATOCRIT'
|
||||
WHEN itemid = 50811 and valuenum > 50 THEN null -- g/dL 'HEMOGLOBIN'
|
||||
WHEN itemid = 51222 and valuenum > 50 THEN null -- g/dL 'HEMOGLOBIN'
|
||||
WHEN itemid = 50813 and valuenum > 50 THEN null -- mmol/L 'LACTATE'
|
||||
WHEN itemid = 51265 and valuenum > 10000 THEN null -- K/uL 'PLATELET'
|
||||
WHEN itemid = 50822 and valuenum > 30 THEN null -- mEq/L 'POTASSIUM'
|
||||
WHEN itemid = 50971 and valuenum > 30 THEN null -- mEq/L 'POTASSIUM'
|
||||
WHEN itemid = 51275 and valuenum > 150 THEN null -- sec 'PTT'
|
||||
WHEN itemid = 51237 and valuenum > 50 THEN null -- 'INR'
|
||||
WHEN itemid = 51274 and valuenum > 150 THEN null -- sec 'PT'
|
||||
WHEN itemid = 50824 and valuenum > 200 THEN null -- mEq/L == mmol/L 'SODIUM'
|
||||
WHEN itemid = 50983 and valuenum > 200 THEN null -- mEq/L == mmol/L 'SODIUM'
|
||||
WHEN itemid = 51006 and valuenum > 300 THEN null -- 'BUN'
|
||||
WHEN itemid = 51300 and valuenum > 1000 THEN null -- 'WBC'
|
||||
WHEN itemid = 51301 and valuenum > 1000 THEN null -- 'WBC'
|
||||
ELSE le.valuenum
|
||||
END as valuenum
|
||||
|
||||
FROM icustays ie
|
||||
|
||||
LEFT JOIN labevents le
|
||||
ON le.subject_id = ie.subject_id AND le.hadm_id = ie.hadm_id
|
||||
AND le.charttime BETWEEN (DATETIME_SUB(ie.intime, INTERVAL '6' HOUR)) AND (DATETIME_ADD(ie.intime, INTERVAL '1' DAY))
|
||||
AND le.ITEMID in
|
||||
(
|
||||
-- comment is: LABEL | CATEGORY | FLUID | NUMBER OF ROWS IN LABEVENTS
|
||||
50868, -- ANION GAP | CHEMISTRY | BLOOD | 769895
|
||||
50862, -- ALBUMIN | CHEMISTRY | BLOOD | 146697
|
||||
51144, -- BANDS - hematology
|
||||
50882, -- BICARBONATE | CHEMISTRY | BLOOD | 780733
|
||||
50885, -- BILIRUBIN, TOTAL | CHEMISTRY | BLOOD | 238277
|
||||
50912, -- CREATININE | CHEMISTRY | BLOOD | 797476
|
||||
50902, -- CHLORIDE | CHEMISTRY | BLOOD | 795568
|
||||
50806, -- CHLORIDE, WHOLE BLOOD | BLOOD GAS | BLOOD | 48187
|
||||
50931, -- GLUCOSE | CHEMISTRY | BLOOD | 748981
|
||||
50809, -- GLUCOSE | BLOOD GAS | BLOOD | 196734
|
||||
51221, -- HEMATOCRIT | HEMATOLOGY | BLOOD | 881846
|
||||
50810, -- HEMATOCRIT, CALCULATED | BLOOD GAS | BLOOD | 89715
|
||||
51222, -- HEMOGLOBIN | HEMATOLOGY | BLOOD | 752523
|
||||
50811, -- HEMOGLOBIN | BLOOD GAS | BLOOD | 89712
|
||||
50813, -- LACTATE | BLOOD GAS | BLOOD | 187124
|
||||
51265, -- PLATELET COUNT | HEMATOLOGY | BLOOD | 778444
|
||||
50971, -- POTASSIUM | CHEMISTRY | BLOOD | 845825
|
||||
50822, -- POTASSIUM, WHOLE BLOOD | BLOOD GAS | BLOOD | 192946
|
||||
51275, -- PTT | HEMATOLOGY | BLOOD | 474937
|
||||
51237, -- INR(PT) | HEMATOLOGY | BLOOD | 471183
|
||||
51274, -- PT | HEMATOLOGY | BLOOD | 469090
|
||||
50983, -- SODIUM | CHEMISTRY | BLOOD | 808489
|
||||
50824, -- SODIUM, WHOLE BLOOD | BLOOD GAS | BLOOD | 71503
|
||||
51006, -- UREA NITROGEN | CHEMISTRY | BLOOD | 791925
|
||||
51301, -- WHITE BLOOD CELLS | HEMATOLOGY | BLOOD | 753301
|
||||
51300 -- WBC COUNT | HEMATOLOGY | BLOOD | 2371
|
||||
)
|
||||
AND valuenum IS NOT null AND valuenum > 0 -- lab values cannot be 0 and cannot be negative
|
||||
) pvt
|
||||
GROUP BY pvt.subject_id, pvt.hadm_id, pvt.icustay_id
|
||||
ORDER BY pvt.subject_id, pvt.hadm_id, pvt.icustay_id;
|
||||
58
sql/firstday/urine_output_first_day.sql
Normal file
58
sql/firstday/urine_output_first_day.sql
Normal file
@@ -0,0 +1,58 @@
|
||||
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
|
||||
DROP TABLE IF EXISTS urine_output_first_day; CREATE TABLE urine_output_first_day AS
|
||||
-- ------------------------------------------------------------------
|
||||
-- Purpose: Create a view of the urine output for each ICUSTAY_ID over the first 24 hours.
|
||||
-- ------------------------------------------------------------------
|
||||
|
||||
select
|
||||
-- patient identifiers
|
||||
ie.subject_id, ie.hadm_id, ie.icustay_id
|
||||
|
||||
-- volumes associated with urine output ITEMIDs
|
||||
, sum(
|
||||
-- we consider input of GU irrigant as a negative volume
|
||||
case
|
||||
when oe.itemid = 227488 and oe.value > 0 then -1*oe.value
|
||||
else oe.value
|
||||
end) as urineoutput
|
||||
FROM icustays ie
|
||||
-- Join to the outputevents table to get urine output
|
||||
left join outputevents oe
|
||||
-- join on all patient identifiers
|
||||
on ie.subject_id = oe.subject_id and ie.hadm_id = oe.hadm_id and ie.icustay_id = oe.icustay_id
|
||||
-- and ensure the data occurs during the first day
|
||||
and oe.charttime between ie.intime and (DATETIME_ADD(ie.intime, INTERVAL '1' DAY)) -- first ICU day
|
||||
where itemid in
|
||||
(
|
||||
-- these are the most frequently occurring urine output observations in CareVue
|
||||
40055, -- "Urine Out Foley"
|
||||
43175, -- "Urine ."
|
||||
40069, -- "Urine Out Void"
|
||||
40094, -- "Urine Out Condom Cath"
|
||||
40715, -- "Urine Out Suprapubic"
|
||||
40473, -- "Urine Out IleoConduit"
|
||||
40085, -- "Urine Out Incontinent"
|
||||
40057, -- "Urine Out Rt Nephrostomy"
|
||||
40056, -- "Urine Out Lt Nephrostomy"
|
||||
40405, -- "Urine Out Other"
|
||||
40428, -- "Urine Out Straight Cath"
|
||||
40086,-- Urine Out Incontinent
|
||||
40096, -- "Urine Out Ureteral Stent #1"
|
||||
40651, -- "Urine Out Ureteral Stent #2"
|
||||
|
||||
-- these are the most frequently occurring urine output observations in MetaVision
|
||||
226559, -- "Foley"
|
||||
226560, -- "Void"
|
||||
226561, -- "Condom Cath"
|
||||
226584, -- "Ileoconduit"
|
||||
226563, -- "Suprapubic"
|
||||
226564, -- "R Nephrostomy"
|
||||
226565, -- "L Nephrostomy"
|
||||
226567, -- Straight Cath
|
||||
226557, -- R Ureteral Stent
|
||||
226558, -- L Ureteral Stent
|
||||
227488, -- GU Irrigant Volume In
|
||||
227489 -- GU Irrigant/Urine Volume Out
|
||||
)
|
||||
group by ie.subject_id, ie.hadm_id, ie.icustay_id
|
||||
order by ie.subject_id, ie.hadm_id, ie.icustay_id;
|
||||
120
sql/firstday/vitals_first_day.sql
Normal file
120
sql/firstday/vitals_first_day.sql
Normal file
@@ -0,0 +1,120 @@
|
||||
-- THIS SCRIPT IS AUTOMATICALLY GENERATED. DO NOT EDIT IT DIRECTLY.
|
||||
DROP TABLE IF EXISTS vitals_first_day; CREATE TABLE vitals_first_day AS
|
||||
-- This query pivots the vital signs for the first 24 hours of a patient's stay
|
||||
-- Vital signs include heart rate, blood pressure, respiration rate, and temperature
|
||||
|
||||
SELECT pvt.subject_id, pvt.hadm_id, pvt.icustay_id
|
||||
|
||||
-- Easier names
|
||||
, min(case when VitalID = 1 then valuenum ELSE NULL END) AS heartrate_min
|
||||
, max(case when VitalID = 1 then valuenum ELSE NULL END) AS heartrate_max
|
||||
, avg(case when VitalID = 1 then valuenum ELSE NULL END) AS heartrate_mean
|
||||
, min(case when VitalID = 2 then valuenum ELSE NULL END) AS sysbp_min
|
||||
, max(case when VitalID = 2 then valuenum ELSE NULL END) AS sysbp_max
|
||||
, avg(case when VitalID = 2 then valuenum ELSE NULL END) AS sysbp_mean
|
||||
, min(case when VitalID = 3 then valuenum ELSE NULL END) AS diasbp_min
|
||||
, max(case when VitalID = 3 then valuenum ELSE NULL END) AS diasbp_max
|
||||
, avg(case when VitalID = 3 then valuenum ELSE NULL END) AS diasbp_mean
|
||||
, min(case when VitalID = 4 then valuenum ELSE NULL END) AS meanbp_min
|
||||
, max(case when VitalID = 4 then valuenum ELSE NULL END) AS meanbp_max
|
||||
, avg(case when VitalID = 4 then valuenum ELSE NULL END) AS meanbp_mean
|
||||
, min(case when VitalID = 5 then valuenum ELSE NULL END) AS resprate_min
|
||||
, max(case when VitalID = 5 then valuenum ELSE NULL END) AS resprate_max
|
||||
, avg(case when VitalID = 5 then valuenum ELSE NULL END) AS resprate_mean
|
||||
, min(case when VitalID = 6 then valuenum ELSE NULL END) AS tempc_min
|
||||
, max(case when VitalID = 6 then valuenum ELSE NULL END) AS tempc_max
|
||||
, avg(case when VitalID = 6 then valuenum ELSE NULL END) AS tempc_mean
|
||||
, min(case when VitalID = 7 then valuenum ELSE NULL END) AS spo2_min
|
||||
, max(case when VitalID = 7 then valuenum ELSE NULL END) AS spo2_max
|
||||
, avg(case when VitalID = 7 then valuenum ELSE NULL END) AS spo2_mean
|
||||
, min(case when VitalID = 8 then valuenum ELSE NULL END) AS glucose_min
|
||||
, max(case when VitalID = 8 then valuenum ELSE NULL END) AS glucose_max
|
||||
, avg(case when VitalID = 8 then valuenum ELSE NULL END) AS glucose_mean
|
||||
|
||||
FROM (
|
||||
select ie.subject_id, ie.hadm_id, ie.icustay_id
|
||||
, case
|
||||
when itemid in (211,220045) and valuenum > 0 and valuenum < 300 then 1 -- HeartRate
|
||||
when itemid in (51,442,455,6701,220179,220050) and valuenum > 0 and valuenum < 400 then 2 -- SysBP
|
||||
when itemid in (8368,8440,8441,8555,220180,220051) and valuenum > 0 and valuenum < 300 then 3 -- DiasBP
|
||||
when itemid in (456,52,6702,443,220052,220181,225312) and valuenum > 0 and valuenum < 300 then 4 -- MeanBP
|
||||
when itemid in (615,618,220210,224690) and valuenum > 0 and valuenum < 70 then 5 -- RespRate
|
||||
when itemid in (223761,678) and valuenum > 70 and valuenum < 120 then 6 -- TempF, converted to degC in valuenum call
|
||||
when itemid in (223762,676) and valuenum > 10 and valuenum < 50 then 6 -- TempC
|
||||
when itemid in (646,220277) and valuenum > 0 and valuenum <= 100 then 7 -- SpO2
|
||||
when itemid in (807,811,1529,3745,3744,225664,220621,226537) and valuenum > 0 then 8 -- Glucose
|
||||
|
||||
else null end as vitalid
|
||||
-- convert F to C
|
||||
, case when itemid in (223761,678) then (valuenum-32)/1.8 else valuenum end as valuenum
|
||||
|
||||
from icustays ie
|
||||
left join chartevents ce
|
||||
on ie.icustay_id = ce.icustay_id
|
||||
and ce.charttime between ie.intime and DATETIME_ADD(ie.intime, INTERVAL '1' DAY)
|
||||
and DATETIME_DIFF(ce.charttime, ie.intime, 'SECOND') > 0
|
||||
and DATETIME_DIFF(ce.charttime, ie.intime, 'HOUR') <= 24
|
||||
-- exclude rows marked as error
|
||||
and (ce.error IS NULL or ce.error = 0)
|
||||
where ce.itemid in
|
||||
(
|
||||
-- HEART RATE
|
||||
211, --"Heart Rate"
|
||||
220045, --"Heart Rate"
|
||||
|
||||
-- Systolic/diastolic
|
||||
|
||||
51, -- Arterial BP [Systolic]
|
||||
442, -- Manual BP [Systolic]
|
||||
455, -- NBP [Systolic]
|
||||
6701, -- Arterial BP #2 [Systolic]
|
||||
220179, -- Non Invasive Blood Pressure systolic
|
||||
220050, -- Arterial Blood Pressure systolic
|
||||
|
||||
8368, -- Arterial BP [Diastolic]
|
||||
8440, -- Manual BP [Diastolic]
|
||||
8441, -- NBP [Diastolic]
|
||||
8555, -- Arterial BP #2 [Diastolic]
|
||||
220180, -- Non Invasive Blood Pressure diastolic
|
||||
220051, -- Arterial Blood Pressure diastolic
|
||||
|
||||
|
||||
-- MEAN ARTERIAL PRESSURE
|
||||
456, --"NBP Mean"
|
||||
52, --"Arterial BP Mean"
|
||||
6702, -- Arterial BP Mean #2
|
||||
443, -- Manual BP Mean(calc)
|
||||
220052, --"Arterial Blood Pressure mean"
|
||||
220181, --"Non Invasive Blood Pressure mean"
|
||||
225312, --"ART BP mean"
|
||||
|
||||
-- RESPIRATORY RATE
|
||||
618,-- Respiratory Rate
|
||||
615,-- Resp Rate (Total)
|
||||
220210,-- Respiratory Rate
|
||||
224690, -- Respiratory Rate (Total)
|
||||
|
||||
|
||||
-- SPO2, peripheral
|
||||
646, 220277,
|
||||
|
||||
-- GLUCOSE, both lab and fingerstick
|
||||
807,-- Fingerstick Glucose
|
||||
811,-- Glucose (70-105)
|
||||
1529,-- Glucose
|
||||
3745,-- BloodGlucose
|
||||
3744,-- Blood Glucose
|
||||
225664,-- Glucose finger stick
|
||||
220621,-- Glucose (serum)
|
||||
226537,-- Glucose (whole blood)
|
||||
|
||||
-- TEMPERATURE
|
||||
223762, -- "Temperature Celsius"
|
||||
676, -- "Temperature C"
|
||||
223761, -- "Temperature Fahrenheit"
|
||||
678 -- "Temperature F"
|
||||
|
||||
)
|
||||
) pvt
|
||||
group by pvt.subject_id, pvt.hadm_id, pvt.icustay_id
|
||||
order by pvt.subject_id, pvt.hadm_id, pvt.icustay_id;
|
||||
Reference in New Issue
Block a user