select
    organ_code, organ_name, comm_id, comm_name
     , date_format(TaskEndTime, '%Y%m') task_ym
     , sum(case when TaskStatue = 2 then 1 else 0 end) task_finish_num  -- 完成数量
     , count(*) task_num    -- 任务数量
     ,sum(case when TaskStatue = 2 then 1 else 0 end) / count(*) * 100 as rate
from dwd_engine_equipment_inspection_task_d a
           join (select distinct organ_code, organ_name, comm_id, comm_name from dim_organ_mapping) b
                   on a.CommId = b.comm_id
group by
    organ_code, organ_name, comm_id, comm_name
       , date_format(TaskEndTime, '%Y%m')