select
    b.organ_code
     , b.organ_name
     , b.comm_id
     , b.comm_name
     , date_format(a.IncidentDate,'%Y%m') incident_ym
     , count(*) incident_num
     , sum(case when ifnull(a.CloseTime,0)=0 then 0 else 1 end) incident_close_num
     , sum(case when a.IsClose=0 and CloseTime is null and a.MainEndDate is null then 1 else 0 end) processing_incident_num
     , sum(case when a.CloseTime is null and MainEndDate is not null then 1 else 0 end) completed_not_close_incident_num
     , sum(case when timestampdiff(hour,a.ForwardDateTime, a.IncidentDate)>2 then 1 else 0 end) response_timeout_incident_num
     , sum(case when timestampdiff(hour,a.ForwardDateTime, a.IncidentDate)<=2 then 1 else 0 end) response_timely_incident_num
     , sum(case when date_format(a.DispDate,'%Y%m')=date_format(a.IncidentDate,'%Y%m') and a.MainEndDate is not null then 1 else 0 end) completed_incident_num
     , sum(case when a.IsClose=1 and a.ReplyResult='1' then 1 else 0 end) follow_up_incident_num
     , sum(case when a.IsClose=1 and a.ReplyResult='1' and a.ServiceQuality in ('满意','非常满意') then 1 else 0 end) follow_up_satisfied_incident_num
     , sum(case when a.IsBeoverdue = '否' then 1 else 0 end) not_overdue_incident_num
     , sum(case when a.DelayHours is not null then 1 else 0 end) delay_incident_num
     , sum(case when a.IsTouSu = 1 then 1 else 0 end) tousu_incident_num
     , sum(case when a.IsClose=1 and a.IsTouSu = 1 then 1 else 0 end) tousu_close_incident_num
     , sum(case when ifnull(MainEndDate,'') = '' and IFNULL(CloseTime,'') = '' AND IsClose = 0 then 1 else 0 end) indeal_incident_num
from dwd_estate_incident_info_d a
         inner join (select distinct organ_code, organ_name, comm_id, comm_name from dim_organ_mapping) b
                    on a.CommID = b.comm_id
where date_format(a.IncidentDate,'%Y%m') = date_format(a.ReserveDate,'%Y%m')
group by 1,2,3,4,5