-- 【report_设备完好率明细】
SELECT
T.name as name
,T.good_rate as good_rate
FROM
(
SELECT
${if(len(p_area)>0, "comm_name name","organ_name name")}
-- ,SUM(good_equipment_num) AS good_equipment_num -- 完好设备数
-- ,SUM(equipment_num) AS equipment_num-- 设备总数
,CASE WHEN SUM(equipment_num) = 0 THEN 0
      ELSE SUM(good_equipment_num) / SUM(equipment_num)
      END as good_rate
FROM dw.dws_engine_equipment_in_good_d
where 1=1
${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
group by
${if(len(p_area)>0, "comm_name","organ_name")}
)T
-- ORDER BY T.good_rate desc



-- 【report_设备完好率按片区top10】


SELECT
    T.organ_name as name
     ,T.good_rate as good_rate

FROM
    (
        SELECT
         --   comm_name
            organ_name
-- ,SUM(good_equipment_num) AS good_equipment_num -- 完好设备数
-- ,SUM(equipment_num) AS equipment_num-- 设备总数
             ,CASE WHEN SUM(equipment_num) = 0 THEN 0
                   ELSE SUM(good_equipment_num) / SUM(equipment_num)
            END as good_rate
        FROM dw.dws_engine_equipment_in_good_d
        where 1=1
        ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
        ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
        group by
            organ_name
    )T
ORDER BY T.good_rate desc
limit 10



-- 【report_设备完好率按项目top10】




SELECT
    T.comm_name as name
     ,T.good_rate as good_rate

FROM
    (
        SELECT
              comm_name
           -- organ_name
-- ,SUM(good_equipment_num) AS good_equipment_num -- 完好设备数
-- ,SUM(equipment_num) AS equipment_num-- 设备总数
             ,CASE WHEN SUM(equipment_num) = 0 THEN 0
                   ELSE SUM(good_equipment_num) / SUM(equipment_num)
            END as good_rate
        FROM dw.dws_engine_equipment_in_good_d
        where 1=1
            ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
              ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
        group by
            comm_name
    )T
ORDER BY T.good_rate desc
limit 10
