select
    district_name
     , project_name
     , district_code
     , project_code
     , left(fee_date,7) fee_ym
     , sum(case when status_name not in ('正常','完结') then owed_amt else 0 end) owed_amt
     , sum(case when status_name in ('待移交','催收中','不移交')
    and timestampdiff(month, str_to_date(fee_date,'%Y-%m-%d'), curdate())>24
                    then owed_amt else 0 end) morethan_24m_not_trans_amt
     , count(distinct case when status_name not in ('正常','完结') then belong_resource else null end) owed_num
     , count(distinct case when status_name in ('待移交','催收中','不移交')
    and timestampdiff(month, str_to_date(fee_date,'%Y-%m-%d'), curdate())>24
                               then belong_resource else null end) morethan_24m_not_trans_num
from dwd_law_owed_info_d
group by
    district_name
       , project_name
       , district_code
       , project_code
       , left(fee_date,7)