 
   from
   (
-    select hadm_id, drg_type, cast(drg_code as numeric) as drg_code from mimiciii.drgcodes where drg_type = 'HCFA'
+    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
+  select hadm_id, subject_id
 , max(carddrg) as carddrg
 , max(peridrg) as peridrg
 , max(renaldrg) as renaldrg


from
(
  select hadm_id, drg_type, cast(drg_code as numeric) as drg_code from mimiciii.drgcodes where drg_type = 'MS'
) d

)
, hcfadrg as
(
select
  hadm_id

  /** V24 DRG Formats  **/


  , max(psych) as psych
  , max(depress) as depress
from eliflg
group by hadm_id, subject_id
)

-- DRG FILTER --
, msdrg as
(
select
  hadm_id, subject_id
/**** V29 MS-DRG Formats ****/

/* Cardiac */
