-- 【1_report_巡查完成率明细】


SELECT
T.name    AS name
,T.task_finish_rate AS task_finish_rate
FROM
(
select
${IF(LEN(p_area)>0,"comm_name name","organ_name name")}
,task_ym
,CASE WHEN sum(task_num) = 0 THEN 0
      ELSE sum(task_finish_num) / sum(task_num) END AS task_finish_rate
FROM dw.dws_engine_equipment_inspect_task_m
where 1=1
${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
${IF(LEN(p_ym)>0," AND task_ym ='" + left(p_ym,4) + right(p_ym,2) + "'","")}
${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
group by
${IF(LEN(p_area)>0,"comm_name","organ_name")}
,task_ym
)T
-- order by  task_finish_rate


-- 【2_report_巡查完成率黑榜_按片区】




SELECT
    T.name    AS name
     ,T.task_finish_rate AS task_finish_rate
FROM
    (
        select
             organ_name name
             ,task_ym
             ,CASE WHEN sum(task_num) = 0 THEN 0
                   ELSE sum(task_finish_num) / sum(task_num) END AS task_finish_rate
        FROM dw.dws_engine_equipment_inspect_task_m
        where 1=1
            ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
              ${IF(LEN(p_ym)>0," AND task_ym ='" + left(p_ym,4) + right(p_ym,2) + "'","")}
              ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
        group by
            organ_name
               ,task_ym
    )T
order by  task_finish_rate
limit 10


-- 【3_report_巡查完成率黑榜_按项目】




SELECT
    T.name    AS name
     ,T.task_finish_rate AS task_finish_rate
FROM
    (
        select
            comm_name name
             ,task_ym
             ,CASE WHEN sum(task_num) = 0 THEN 0
                   ELSE sum(task_finish_num) / sum(task_num) END AS task_finish_rate
        FROM dw.dws_engine_equipment_inspect_task_m
        where 1=1
            ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
              ${IF(LEN(p_ym)>0," AND task_ym ='" + left(p_ym,4) + right(p_ym,2) + "'","")}
              ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
        group by
          comm_name
               ,task_ym
    )T
order by  task_finish_rate
limit 10