 ,
 eliflg as
 (
-select hadm_id, seq_num, icd9_code
+select hadm_id, subject_id, seq_num, icd9_code
 -- note that these codes will seem incomplete at first
 -- for example, CHF is missing a lot of codes referenced in the literature (402.11, 402.91, etc)
 -- these codes are captured by hypertension flags instead


from mimiciii.admissions adm
left join eligrp eli
  on adm.hadm_id = eli.hadm_id
left join drggrp d
  on adm.hadm_id = d.hadm_id
order by adm.hadm_id;

-- this groups comorbidities together for a single patient admission
, eligrp as
(
  select hadm_id, subject_id
  , max(chf) as chf
  , max(arythm) as arythm
  , max(valve) as valve
