select
    c.organ_code, c.organ_name, c.comm_id, c.comm_name
     , sum(case when ifnull(EquipmentStatus,'正常中') = '正常中' then 1 else 0 end) good_equipment_num
     , count(*) equipment_num
-- , a.*, b.*
from dim_engine_equipment_d a
         left join (
    select
        EquipmentId, EquipmentStatus, AddTime, rn
    from (
             select EquipmentId, EquipmentStatus, AddTime
                  , row_number() over (partition by EquipmentId order by AddTime desc) rn
             from dwd_engine_equipment_status_d
         ) a
    where rn = 1
) b
                   on a.EquipmentId = b.EquipmentId
         inner join (select distinct organ_code, organ_name, comm_id, comm_name from dim_organ_mapping) c
                    on a.CommID = c.comm_id
group by
    c.organ_code, c.organ_name, c.comm_id, c.comm_name

;
SELECT * FROM dim_engine_equipment_d where SystemId = '8ae5eea4-f0e0-4106-a022-ad2b370cad80'