   , max(psych) as psych
   , max(depress) as depress
 from eliflg
-group by hadm_id
+group by hadm_id, subject_id
 )
 
 -- DRG FILTER --
 , msdrg as
 (
 select
-  hadm_id
+  hadm_id, subject_id
 /**** V29 MS-DRG Formats ****/
 
 /* Cardiac */

  UNION
  select d1.* from hcfadrg d1
) d
group by d.hadm_id
)
-- now merge these flags together to define elixhauser
-- most are straightforward.. but hypertension flags are a bit more complicated
select adm.hadm_id
, case
    when carddrg = 1 then 0 -- DRG filter



  from
  (
    select hadm_id, subject_id, drg_type, cast(drg_code as numeric) as drg_code from mimiciii.drgcodes where drg_type = 'HCFA'
  ) d
)
-- merge DRG groups together
, drggrp as
(
  select hadm_id, subject_id
, max(carddrg) as carddrg
, max(peridrg) as peridrg
, max(renaldrg) as renaldrg
