-- ======================================
-- 指标卡_在职人数
-- ======================================
SELECT
    SUM(zzs) AS SL
FROM
(
    SELECT
    A1.pq
 --   ,A1.xm
    ,SUM(A1.zzs) as zzs
    FROM
    (
    SELECT

           pq  -- 片区
       --    ,xm  -- 项目
           ,ny  AS ym
           ,zzs  -- 在职人数
      FROM ods_hr_view_mbl_d --
      WHERE ny > '2024-10'
      -- 新增历史数据
    UNION ALL
     SELECT
        organ_code as pq
     ,concat(left(ym,4),'-',right(ym,2)) as ym
     ,SUM(zzs) AS zzs
  --  ,SUM(bzs) AS bzs
     FROM ods_hr_history_bz_num
     WHERE 1 = 1
     GROUP BY organ_code,concat(left(ym,4),'-',right(ym,2))
    )A1
        WHERE A1.ym = '${p_ym}'
        GROUP BY A1.pq
    )A
    LEFT JOIN
    (
        SELECT
            organ_code
             ,organ_name
             ,hr_organ_id
          --   ,hr_organ_name
          --   ,hr_comm_id
          --   ,hr_comm_name
          --   ,comm_name
        FROM dim_organ_mapping
        WHERE length(hr_comm_id) > 0
        GROUP BY
            organ_code
               ,organ_name
               ,hr_organ_id
             --  ,hr_organ_name
             --  ,hr_comm_id
             --  ,hr_comm_name
             --  ,comm_name
        UNION ALL
        SELECT
            '0000' AS organ_code    -- erp片区编码
             ,''    AS organ_name  -- erp片区名称
             ,'153'    AS hr_organ_id  -- hr片区编码
           --  ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
           --  ,'153'    AS hr_comm_id    -- hr项目id
           --  ,'领悦集团总部'    AS hr_comm_name  -- hr项目名称
           -- ,''     AS comm_name
    )B
    ON A.pq = B.hr_organ_id
 --   and A.xm = b.hr_comm_id
where 1 = 1
${IF(LEN(p_area)>0," AND B.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
-- ${IF(LEN(p_shop)!=0," and B.hr_comm_name IN ('"+JOINARRAY(p_shop,"','")+"')","")}

-- ======================================
-- 指标卡_离职人数
-- ======================================


SELECT
    SUM(lzrs) AS SL
FROM
(
    SELECT
     A1.pq AS pq
    ,A1.xm AS xm
    ,SUM(A1.zlzrs) AS lzrs
    FROM
    (
            SELECT pq                        -- 片区
           , xm                        -- 项目
           , CONCAT(nd, '-', yd) AS ym -- 年月
           , zlzrs                      -- 离职人数
        FROM ods_hr_view_lzbl_d -- 离职人数
        WHERE LEFT(rq, 7) >= '2024-10' -- 取累计值 10月以后取HR系统
        UNION ALL
        SELECT
        T1.organ_code  AS pq
         ,T1.comm_id     AS xm
         ,T1.ym          AS ym
         ,SUM(T1.active_quit) + SUM(T1.pass_quit  ) AS zlzrs
        FROM
        (
            SELECT
                organ_code
                 ,organ_name
                 ,comm_id
                 ,comm_name
                 ,'2024-01' AS ym
                 ,COALESCE(jan_active_quit,0)    AS active_quit -- 主动离职人数
                 ,COALESCE(jan_pass_quit,0)      AS pass_quit  -- 被动离职人数
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT
                organ_code
                 ,organ_name
                 ,comm_id
                 ,comm_name
                 ,'2024-02' AS ym
                 ,COALESCE(feb_active_quit,0)    AS active_quit -- 主动离职人数
                 ,COALESCE(feb_pass_quit,0)      AS pass_quit  -- 被动离职人数
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT
                organ_code
                 ,organ_name
                 ,comm_id
                 ,comm_name
                 ,'2024-03' AS ym
                 ,COALESCE(mar_active_quit,0)    AS active_quit -- 主动离职人数
                 ,COALESCE(mar_pass_quit,0)      AS pass_quit  -- 被动离职人数
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT
                organ_code
                 ,organ_name
                 ,comm_id
                 ,comm_name
                 ,'2024-04' AS ym

                 ,COALESCE(apr_active_quit,0)    AS active_quit -- 主动离职人数
                 ,COALESCE(apr_pass_quit,0)      AS pass_quit  -- 被动离职人数
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT
                organ_code
                 ,organ_name
                 ,comm_id
                 ,comm_name
                 ,'2024-05' AS ym

                 ,COALESCE(may_active_quit,0)    AS active_quit -- 主动离职人数
                 ,COALESCE(may_pass_quit,0)      AS pass_quit  -- 被动离职人数
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT
                organ_code
                 ,organ_name
                 ,comm_id
                 ,comm_name
                 ,'2024-06' AS ym
                 ,COALESCE(jun_active_quit,0)    AS active_quit -- 主动离职人数
                 ,COALESCE(jun_pass_quit,0)      AS pass_quit  -- 被动离职人数
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT
                organ_code
                 ,organ_name
                 ,comm_id
                 ,comm_name
                 ,'2024-07' AS ym
                 ,COALESCE(jul_active_quit,0)    AS active_quit -- 主动离职人数
                 ,COALESCE(jul_pass_quit,0)      AS pass_quit  -- 被动离职人数
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT
                organ_code
                 ,organ_name
                 ,comm_id
                 ,comm_name
                 ,'2024-08' AS ym
                 ,COALESCE(aug_active_quit,0)    AS active_quit -- 主动离职人数
                 ,COALESCE(aug_pass_quit,0)      AS pass_quit  -- 被动离职人数
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT
                organ_code
                 ,organ_name
                 ,comm_id
                 ,comm_name
                 ,'2024-09' AS ym
                 ,COALESCE(sep_active_quit,0)    AS active_quit -- 主动离职人数
                 ,COALESCE(sep_pass_quit,0)      AS pass_quit  -- 被动离职人数
            FROM ods_hr_history_erp_date
        )T1
        GROUP BY T1.organ_code, T1.comm_id ,T1.ym
     )A1
        WHERE  left(A1.ym,4) =left('${p_ym}',4)
        AND left(A1.ym,7) <= '${p_ym}'
        AND pq not in('89','267') -- 排除了佳美、悦汇
    GROUP BY  A1.pq,A1.xm
    )A
        LEFT JOIN
        (
            SELECT
                organ_code
                 ,organ_name
                 ,hr_organ_id
                 ,hr_organ_name
                 ,hr_comm_id
                 ,hr_comm_name
                 ,comm_name
            FROM dim_organ_mapping
            WHERE length(hr_comm_id) > 0
            GROUP BY
                organ_code
                   ,organ_name
                   ,hr_organ_id
                   ,hr_organ_name
                   ,hr_comm_id
                   ,hr_comm_name
                   ,comm_name
            UNION ALL
            SELECT
                '0000' AS organ_code    -- erp片区编码
                 ,''    AS organ_name  -- erp片区名称
                 ,'153'    AS hr_organ_id  -- hr片区编码
                 ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
                 ,'153'    AS hr_comm_id    -- hr项目id
                 ,'领悦集团总部'    AS hr_comm_name  -- hr项目名称
                 ,''     AS comm_name
    )B
    ON A.pq = B.hr_organ_id
    and A.xm = B.hr_comm_id
where 1 = 1
${IF(LEN(p_area)>0," AND B.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
 ${IF(LEN(p_shop)!=0," and B.hr_comm_name IN ('"+JOINARRAY(p_shop,"','")+"')","")}



-- ======================================
-- 指标卡_人力行政成本
-- ======================================

SELECT
coalesce(SUM(CASE WHEN A.cost_type = '人力成本' THEN A.actual_amount ELSE 0 END),0) AS lc_total_fee -- 人力成本费用
,coalesce(SUM(CASE WHEN A.cost_type = '行政成本' THEN A.actual_amount ELSE 0 END),0) AS ac_total_fee  -- 行政成本费用
FROM
(
    SELECT
        *
    FROM dw.ods_cost_collection_lc_ac
    WHERE  left(ym,4) =  left('${p_ym}',4)
    AND ym <= '${p_ym}'
)A
    LEFT JOIN
(
    SELECT
        organ_code
         ,organ_name
         ,hr_organ_id
         ,hr_organ_name
         ,hr_comm_id
         ,hr_comm_name
         ,comm_name
    FROM dim_organ_mapping
    WHERE length(hr_comm_id) > 0
    GROUP BY
        organ_code
           ,organ_name
           ,hr_organ_id
           ,hr_organ_name
           ,hr_comm_id
           ,hr_comm_name
           ,comm_name
    UNION ALL
    SELECT
        '0000' AS organ_code    -- erp片区编码
         ,''    AS organ_name  -- erp片区名称
         ,'153'    AS hr_organ_id  -- hr片区编码
         ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
         ,'153'    AS hr_comm_id    -- hr项目id
         ,'领悦集团总部'    AS hr_comm_name  -- hr项目名称
         ,''     AS comm_name
)B
ON A.organ_code = B.organ_code
and A.comm_id = B.hr_comm_id
WHERE 1 = 1
${IF(LEN(p_area)>0," AND B.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
${IF(LEN(p_shop)!=0," and B.hr_comm_name IN ('"+JOINARRAY(p_shop,"','")+"')","")}


-- ======================================
-- 指标卡_离职人数离职率/目标值
-- ======================================

-- 整体
${IF(LEN(p_area) = 0,"", "/*")}

SELECT
sum(lz_rate) AS lz_rate
,sum(lz_rate_target) AS lz_rate_target
,CASE WHEN sum(lz_rate_target) = 0 THEN 0
      ELSE sum(lz_rate) / sum(lz_rate_target) END AS target_rate
FROM
(
SELECT
CASE WHEN  SUM(zzrs) = 0 THEN 0
                   ELSE  SUM(zlzrs) /  SUM(zzrs)
            END AS lz_rate  -- 离职率
,0 AS lz_rate_target        -- 离职率目标
FROM
(
    SELECT
    SUM(A1.zzrs) AS zzrs
    ,SUM(A1.zlzrs) AS zlzrs
    FROM
    (
    SELECT
            LEFT(rq,7) AS ny
            ,(SUM(CASE WHEN rq = CONCAT('${p_ym}','-01') THEN zzrs ELSE 0 END)
                + SUM(CASE WHEN rq = LAST_DAY(CONCAT('${p_ym}','-01')) THEN zzrs ELSE 0 END)) / 2
    AS zzrs	-- 当天在职人数
             ,SUM(zlzrs) AS zlzrs	-- 当天总离职人数
        FROM ods_hr_view_lzbl_d
        WHERE  LEFT(rq,7) >=  '2024-10'
        AND pq not in('89','267') -- 排除了佳美、悦汇
        group by LEFT(rq,7)
        UNION ALL
        SELECT
        T1.ym          AS ny
        ,(SUM(T1.begin_num) + SUM(T1.end_num)) / 2 AS zzrs
        ,SUM(T1.active_quit) + SUM(T1.pass_quit  ) AS zlzrs
        FROM
            (
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-01' AS ym
                     ,COALESCE(jan_active_quit,0)    AS active_quit -- 主动离职人数
                     ,COALESCE(jan_pass_quit,0)      AS pass_quit  -- 被动离职人数
                     ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                     ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-02' AS ym
                     ,COALESCE(feb_active_quit,0)    AS active_quit -- 主动离职人数
                     ,COALESCE(feb_pass_quit,0)      AS pass_quit  -- 被动离职人数
                     ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                     ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-03' AS ym
                     ,COALESCE(mar_active_quit,0)    AS active_quit -- 主动离职人数
                     ,COALESCE(mar_pass_quit,0)      AS pass_quit  -- 被动离职人数
                     ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                    ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-04' AS ym

                     ,COALESCE(apr_active_quit,0)    AS active_quit -- 主动离职人数
                     ,COALESCE(apr_pass_quit,0)      AS pass_quit  -- 被动离职人数
                     ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                     ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职

                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-05' AS ym

                     ,COALESCE(may_active_quit,0)    AS active_quit -- 主动离职人数
                     ,COALESCE(may_pass_quit,0)      AS pass_quit  -- 被动离职人数
                     ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                    ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职

                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-06' AS ym
                     ,COALESCE(jun_active_quit,0)    AS active_quit -- 主动离职人数
                     ,COALESCE(jun_pass_quit,0)      AS pass_quit  -- 被动离职人数
                     ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                    ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职

                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-07' AS ym
                     ,COALESCE(jul_active_quit,0)    AS active_quit -- 主动离职人数
                     ,COALESCE(jul_pass_quit,0)      AS pass_quit  -- 被动离职人数
                     ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                    ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-08' AS ym
                     ,COALESCE(aug_active_quit,0)    AS active_quit -- 主动离职人数
                     ,COALESCE(aug_pass_quit,0)      AS pass_quit  -- 被动离职人数
                     ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                    ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职

                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-09' AS ym
                     ,COALESCE(sep_active_quit,0)    AS active_quit -- 主动离职人数
                     ,COALESCE(sep_pass_quit,0)      AS pass_quit  -- 被动离职人数
                     ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                     ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职

                FROM ods_hr_history_erp_date
            )T1
        GROUP BY  T1.ym
    )A1
     WHERE A1.ny = '${p_ym}'
)A
UNION ALL
SELECT
0  AS lz_rate
,lz_rate_target  -- 离职率目标
FROM ods_hr_collection_target_overall
WHERE ym = '${p_ym}'
)T


${IF(LEN(p_area) = 0,"", "*/")}

-- 片区
${IF(LEN(p_area) > 0,"", "/*")}

SELECT
    CASE WHEN  sum(zzrs) = 0 THEN 0
         ELSE  SUM(zlzrs) / sum(zzrs)
        END AS lz_rate
,sum(C.lz_rate_target) AS lz_rate_target
,CASE WHEN  (sum(zzrs) = 0 OR sum(lz_rate_target)= 0) THEN 0
         ELSE  (SUM(zlzrs) / sum(zzrs)) / sum(lz_rate_target)
        END AS target_rate
FROM
(
      SELECT
     A1.pq AS pq
      ,A1.zzrs AS zzrs
      ,A1.zlzrs AS zlzrs
      FROM
      (
           SELECT
            LEFT(rq, 7)        AS ny
            , pq                  -- 片区
              ,(SUM(CASE WHEN rq = CONCAT('${p_ym}','-01') THEN zzrs ELSE 0 END)
                + SUM(CASE WHEN rq = LAST_DAY(CONCAT('${p_ym}','-01')) THEN zzrs ELSE 0 END)) / 2
    AS zzrs	-- 当天在职人数
           , SUM(zlzrs) AS zlzrs -- 当天总离职人数
      FROM ods_hr_view_lzbl_d
      WHERE LEFT(rq, 7) >=  '2024-10'
      AND pq NOT IN ('89', '267') -- 排除了佳美、悦汇
      -- 排除了:
      -- 89:佳美物业公司
      -- 153:领悦集团总部
      -- 267:悦汇发展公司
      GROUP BY pq,LEFT(rq, 7)
     UNION ALL
      SELECT T1.ym                                   AS ny
           , T1.organ_code                           AS pq
            ,(SUM(T1.begin_num) + SUM(T1.end_num)) / 2 AS zzrs
           , SUM(T1.active_quit) + SUM(T1.pass_quit) AS zlzrs
      FROM (SELECT organ_code
                 , organ_name
                 , comm_id
                 , comm_name
                 , '2024-01'                    AS ym
                 , COALESCE(jan_active_quit, 0) AS active_quit -- 主动离职人数
                 , COALESCE(jan_pass_quit, 0)   AS pass_quit   -- 被动离职人数
                 , COALESCE(sep_begin_num, 0)   AS begin_num   -- 月初在职
                 ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT organ_code
                 , organ_name
                 , comm_id
                 , comm_name
                 , '2024-02'                    AS ym
                 , COALESCE(feb_active_quit, 0) AS active_quit -- 主动离职人数
                 , COALESCE(feb_pass_quit, 0)   AS pass_quit   -- 被动离职人数
                 , COALESCE(sep_begin_num, 0)   AS begin_num   -- 月初在职
                 ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT organ_code
                 , organ_name
                 , comm_id
                 , comm_name
                 , '2024-03'                    AS ym
                 , COALESCE(mar_active_quit, 0) AS active_quit -- 主动离职人数
                 , COALESCE(mar_pass_quit, 0)   AS pass_quit   -- 被动离职人数
                 , COALESCE(sep_begin_num, 0)   AS begin_num   -- 月初在职
                 ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT organ_code
                 , organ_name
                 , comm_id
                 , comm_name
                 , '2024-04'                    AS ym

                 , COALESCE(apr_active_quit, 0) AS active_quit -- 主动离职人数
                 , COALESCE(apr_pass_quit, 0)   AS pass_quit   -- 被动离职人数
                 , COALESCE(sep_begin_num, 0)   AS begin_num   -- 月初在职
                 ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT organ_code
                 , organ_name
                 , comm_id
                 , comm_name
                 , '2024-05'                    AS ym

                 , COALESCE(may_active_quit, 0) AS active_quit -- 主动离职人数
                 , COALESCE(may_pass_quit, 0)   AS pass_quit   -- 被动离职人数
                 , COALESCE(sep_begin_num, 0)   AS begin_num   -- 月初在职
                 ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT organ_code
                 , organ_name
                 , comm_id
                 , comm_name
                 , '2024-06'                    AS ym
                 , COALESCE(jun_active_quit, 0) AS active_quit -- 主动离职人数
                 , COALESCE(jun_pass_quit, 0)   AS pass_quit   -- 被动离职人数
                 , COALESCE(sep_begin_num, 0)   AS begin_num   -- 月初在职
                 ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT organ_code
                 , organ_name
                 , comm_id
                 , comm_name
                 , '2024-07'                    AS ym
                 , COALESCE(jul_active_quit, 0) AS active_quit -- 主动离职人数
                 , COALESCE(jul_pass_quit, 0)   AS pass_quit   -- 被动离职人数
                 , COALESCE(sep_begin_num, 0)   AS begin_num   -- 月初在职
                 ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT organ_code
                 , organ_name
                 , comm_id
                 , comm_name
                 , '2024-08'                    AS ym
                 , COALESCE(aug_active_quit, 0) AS active_quit -- 主动离职人数
                 , COALESCE(aug_pass_quit, 0)   AS pass_quit   -- 被动离职人数
                 , COALESCE(sep_begin_num, 0)   AS begin_num   -- 月初在职
                 ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
            FROM ods_hr_history_erp_date
            UNION ALL
            SELECT organ_code
                 , organ_name
                 , comm_id
                 , comm_name
                 , '2024-09'                    AS ym
                 , COALESCE(sep_active_quit, 0) AS active_quit -- 主动离职人数
                 , COALESCE(sep_pass_quit, 0)   AS pass_quit   -- 被动离职人数
                 , COALESCE(sep_begin_num, 0)   AS begin_num   -- 月初在职
                 ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
            FROM ods_hr_history_erp_date) T1
      GROUP BY T1.ym, T1.organ_code
      )A1
    WHERE  A1.ny = '${p_ym}'
    )A
        LEFT JOIN
    (
        SELECT
            organ_code    -- erp片区编码
             ,organ_name  -- erp片区名称
             ,hr_organ_id  -- hr片区编码
             ,hr_organ_name  -- hr片区名称
        FROM dim_organ_mapping
        WHERE length(hr_organ_id) > 0
        GROUP BY
            organ_code
               ,organ_name
               ,hr_organ_id
               ,hr_organ_name

        UNION ALL
        SELECT
            '0000' AS organ_code    -- erp片区编码
             ,''    AS organ_name  -- erp片区名称
             ,'153'    AS hr_organ_id  -- hr片区编码
             ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
    )B
    ON A.pq = B.hr_organ_id
LEFT JOIN
    (
        SELECT
            organ_code
            ,lz_rate_target
        FROM ods_hr_collection_target_organ
        WHERE ym = '${p_ym}'
    )C
    ON B.organ_code = C.organ_code
where 1 = 1
${IF(LEN(p_area)>0," AND B.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}

${IF(LEN(p_area) > 0,"", "*/")}



-- ======================================
-- 指标卡_在职人数满编率/目标值
-- ======================================



-- 整体
${IF(LEN(p_area) = 0,"", "/*")}
SELECT
sum(T.full_rate) AS full_rate -- 满编率
,sum(T.full_rate_target) AS full_rate_target -- 满编率目标
,CASE WHEN sum(T.full_rate_target) =0 THEN 0
ELSE sum(T.full_rate) / sum(T.full_rate_target) END  AS target_rate -- 目标占比
FROM
(
SELECT
CASE WHEN SUM(bzs) = 0 THEN 0 ELSE SUM(zzs) / SUM(bzs) END AS full_rate -- 满编率
,0 AS full_rate_target -- 满编率目标值
FROM
(
    SELECT
    SUM(A1.bzs) AS bzs
    ,SUM(A1.zzs) AS zzs
    FROM
    (
      SELECT
        ny
        ,zzs -- 在职数
        ,bzs -- 编制数
      FROM ods_hr_view_mbl_d
      WHERE pq NOT IN ('89', '267') -- 排除了佳美、悦汇
      AND ny > '2024-10'
      UNION ALL
       SELECT
      concat(left(ym,4),'-',right(ym,2)) as ny
     ,SUM(zzs) AS zzs
    ,SUM(bzs) AS bzs
     FROM ods_hr_history_bz_num
     WHERE 1 = 1
     GROUP BY ym
    )A1
    WHERE A1.ny = '${p_ym}'
)A
UNION ALL
SELECT
0 AS full_rate -- 满编率
,full_rate_target       -- 满编率目标
FROM ods_hr_collection_target_overall
WHERE ym  = '${p_ym}'
)T
${IF(LEN(p_area)>0," AND B.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}


 ${IF(LEN(p_area) = 0,"", "*/")}

-- 片区
 ${IF(LEN(p_area) > 0,"", "/*")}


SELECT
CASE WHEN SUM(A.bzs) = 0 THEN 0 ELSE SUM(A.zzs) / SUM(A.bzs) END AS full_rate -- 满编率
,SUM(C.full_rate_target) AS full_rate_target -- 满编率目标值
,CASE WHEN (SUM(A.bzs) = 0 OR coalesce(SUM(C.full_rate_target) ,0) = 0)THEN 0
      ELSE (SUM(A.zzs) / SUM(A.bzs)) / SUM(C.full_rate_target)  END AS target_rate -- 满编率目标占比
FROM
(
    SELECT
    A1.ny   AS ny
    ,A1.pq  AS pq
    ,SUM(A1.zzs) AS zzs
    ,sum(A1.bzs)  AS bzs
    FROM
    (
          SELECT
            pq	-- 片区
           ,ny
           ,SUM(zzs) AS zzs 	-- 在职数
           ,SUM(bzs) AS bzs -- 编制数
            FROM ods_hr_view_mbl_d
            WHERE pq not in('89','267') -- 排除了佳美、悦汇
            and ny > '2024-10'
            GROUP BY pq,ny
            UNION ALL
            SELECT
            organ_code as pq
            ,concat(left(ym,4),'-',right(ym,2))  as ny
            ,SUM(zzs) AS zzs
            ,SUM(bzs) AS bzs
          FROM ods_hr_history_bz_num
          WHERE 1 = 1
          GROUP BY ym,organ_code
    )A1
    WHERE A1.ny = '${p_ym}'
    group by     A1.ny,A1.pq
)A
LEFT JOIN
(
        SELECT
            organ_code    -- erp片区编码
             ,organ_name  -- erp片区名称
             ,hr_organ_id  -- hr片区编码
             ,hr_organ_name  -- hr片区名称
        FROM dim_organ_mapping
        WHERE length(hr_organ_id) > 0
        GROUP BY
            organ_code
               ,organ_name
               ,hr_organ_id
               ,hr_organ_name

        UNION ALL
        SELECT
            '0000' AS organ_code    -- erp片区编码
             ,''    AS organ_name  -- erp片区名称
             ,'153'    AS hr_organ_id  -- hr片区编码
             ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
    )B
    ON A.pq = B.hr_organ_id
    LEFT JOIN
    (
        SELECT
        organ_code            -- erp片区code
        ,full_rate_target      -- 满编率目标
        FROM ods_hr_collection_target_organ -- 目标值收集
        WHERE ym = '${p_ym}'
    )C
        ON B.organ_code = C.organ_code

WHERE 1 = 1
    ${IF(LEN(p_area)>0," AND B.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
${IF(LEN(p_area) > 0,"", "*/")}



-- ======================================
-- 指标卡_人力行政成本费率/目标值
-- ======================================

-- 整体
${IF(LEN(p_area)=0,"", "/*")}

SELECT
SUM(T.lc_fee_rate) AS lc_fee_rate -- 人力成本费率
,SUM(T.lc_fee_rate_target)   AS lc_fee_rate_target -- 人力成本费率目标
,CASE WHEN sum(T.lc_fee_rate_target) = 0 THEN 0
      ELSE SUM(T.lc_fee_rate) / sum(T.lc_fee_rate_target) END AS lc_target_rate -- 人力成本费率目标占比
,SUM(T.ac_fee_rate) AS ac_fee_rate  -- 行政成本费率
,SUM(T.ac_fee_rate_target) AS ac_fee_rate_target -- 行政成本费率目标值
,CASE WHEN sum(T.ac_fee_rate_target) = 0 THEN 0
      ELSE sum(T.ac_fee_rate) / sum(T.ac_fee_rate_target) END AS ac_target_rate -- 行政成本费率目标占比
FROM
(
SELECT
coalesce(SUM(CASE WHEN cost_type = '人力成本' THEN actual_amount ELSE 0 END),0)
    / (SELECT SUM(index_income_fact) FROM ods_caiwu_feecollection
                WHERE category = '物业'
                AND yr_month = '${p_ym}' AND organ_name <> '佳美物业') AS lc_fee_rate -- 人力成本费率
,coalesce(SUM(CASE WHEN cost_type = '行政成本' THEN actual_amount ELSE 0 END),0)
    / (SELECT SUM(index_income_fact) FROM ods_caiwu_feecollection
       WHERE category = '物业'
         AND yr_month =  '${p_ym}' AND organ_name <> '佳美物业' )   AS ac_fee_rate  -- 行政成本费率
,0      AS lc_fee_rate_target   -- 人力成本费率
,0      AS ac_fee_rate_target   -- 行政成本费率
from dw.ods_cost_collection_lc_ac
WHERE  left(ym,4) =  left('${p_ym}',4)
  AND ym <= '${p_ym}'
UNION ALL
SELECT
0           AS lc_fee_rate
,0          AS ac_fee_rate
,lc_fee_rate_target   -- 人力成本费率
,ac_fee_rate_target  -- 行政成本费率
FROM ods_hr_collection_target_overall
WHERE ym = '${p_ym}'
)T
${IF(LEN(p_area)=0,"", "*/")}

-- 片区
${IF(LEN(p_area)>0,"", "/*")}
SELECT
    T.lc_fee_rate AS lc_fee_rate -- 人力成本费率
     ,T.lc_fee_rate_target   AS lc_fee_rate_target -- 人力成本费率目标
     ,CASE WHEN T.lc_fee_rate_target = 0 THEN 0
           ELSE T.lc_fee_rate / T.lc_fee_rate_target END AS lc_target_rate -- 人力成本费率目标占比
     ,T.ac_fee_rate AS ac_fee_rate  -- 行政成本费率
     ,T.ac_fee_rate_target AS ac_fee_rate_target -- 行政成本费率目标值
     ,CASE WHEN T.ac_fee_rate_target = 0 THEN 0
           ELSE T.ac_fee_rate / T.ac_fee_rate_target END AS ac_target_rate -- 行政成本费率目标占比
FROM
(
SELECT
         A.lc_total_fee /  B.index_income_fact          AS lc_fee_rate
        ,A.ac_total_fee / B.index_income_fact         AS ac_fee_rate
        ,C.lc_fee_rate_target AS lc_fee_rate_target  -- 人力成本费率
        ,C.ac_fee_rate_target AS ac_fee_rate_target -- 行政成本费率
        FROM
        (
            -- 费控人力行政成本填报
            SELECT
             organ_code
            ,coalesce(SUM(CASE WHEN cost_type = '人力成本' THEN actual_amount ELSE 0 END),0) AS lc_total_fee -- 人力成本费用
            ,coalesce(SUM(CASE WHEN cost_type = '行政成本' THEN actual_amount ELSE 0 END),0) AS ac_total_fee  -- 行政成本费用
            from dw.ods_cost_collection_lc_ac
            WHERE  left(ym,4) =  left('${p_ym}',4)
            AND ym <= '${p_ym}'
            GROUP BY organ_code
        )A
        LEFT JOIN
        (-- 公司实际收入
                SELECT
                 organ_code
                ,SUM(index_income_fact) AS index_income_fact -- 实际收入
                FROM ods_caiwu_feecollection
                WHERE category = '物业'
               AND yr_month = '${p_ym}'
                GROUP BY organ_code
        )B
        ON A.organ_code = B.organ_code
        LEFT JOIN
        (
            SELECT
            organ_code
            ,lc_fee_rate_target    -- 人力成本费率目标
            ,ac_fee_rate_target    -- 行政成本费率目标
            FROM ods_hr_collection_target_organ
            WHERE ym = '${p_ym}'
        )C
        ON A.organ_code = C.organ_code
        WHERE 1 = 1
        ${IF(LEN(p_area)>0," AND A.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
)T
${IF(LEN(p_area)>0,"", "*/")}



-- ======================================
-- 员工满编率柱状图
-- ======================================

SELECT
    yd
  ,SUM(A.zzs) AS zzs	-- 在职数
     ,SUM(A.bzs) AS bzs	-- 编制数
    ,CASE WHEN sum(A.bzs)  = 0 THEN 0
          ELSE sum(A.zzs) / sum(A.bzs)  END AS rate -- 满编率
FROM
(
    SELECT
    A1.yd -- 月度
    ,A1.ny
    ,A1.pq  -- 片区
  --  ,A1.xm  -- 项目
    ,SUM(A1.zzs) AS zzs -- 在职数
    ,sum(A1.bzs) AS bzs  -- 编制数
    FROM
    (
        SELECT
           right(ny,2) AS yd -- 月度
           ,ny
           ,pq  -- 片区
         --  ,xm  -- 项目
           ,zzs  -- 在职数
           ,bzs -- 编制数
      FROM ods_hr_view_mbl_d
    WHERE  left(ny,4) = LEFT('${p_ym}', 4)
      AND ny > '2024-10'
      AND pq NOT IN ('89', '267') -- 排除了佳美、悦汇
      UNION ALL
      SELECT
      right(ym,2) as yd
      ,ym as ny
     ,organ_code as pq
   --  ,''         AS xm
     ,SUM(zzs) AS zzs
    ,SUM(bzs) AS bzs
     FROM ods_hr_history_bz_num
     WHERE 1 = 1
     GROUP BY right(ym,2),ym,organ_code

     /* SELECT
       right(T1.ym,2)   AS yd
      ,T1.ym            AS ny
      ,T1.organ_code  AS pq
      ,T1.comm_id     AS xm
      ,SUM(T1.begin_num  ) AS zzs-- 月初在职
        ,0 AS bzs
        FROM
        (
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-01' AS ym
                     ,COALESCE(jan_begin_num,0)      AS begin_num -- 月初在职

                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-02' AS ym
                     ,COALESCE(feb_begin_num,0)      AS begin_num -- 月初在职
                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-03' AS ym
                     ,COALESCE(mar_begin_num,0)      AS begin_num -- 月初在职
                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-04' AS ym
                     ,COALESCE(apr_begin_num,0)      AS begin_num -- 月初在职
                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-05' AS ym
                     ,COALESCE(may_begin_num,0)      AS begin_num -- 月初在职
                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-06' AS ym
                     ,COALESCE(jun_begin_num,0)      AS begin_num -- 月初在职
                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-07' AS ym
                     ,COALESCE(jul_begin_num,0)      AS begin_num -- 月初在职
                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-08' AS ym
                     ,COALESCE(aug_begin_num,0)      AS begin_num -- 月初在职
                FROM ods_hr_history_erp_date
                UNION ALL
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,'2024-09' AS ym
                     ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                FROM ods_hr_history_erp_date
            )T1
            GROUP BY T1.organ_code, T1.comm_id ,T1.ym*/
     )A1
    WHERE left(A1.ny,4) = LEFT('${p_ym}', 4)
    AND RIGHT(A1.ny,2) <= RIGHT('${p_ym}',2)
group by      A1.yd
    ,A1.ny
    ,A1.pq
)A
LEFT JOIN
(
    SELECT
        organ_code
         ,organ_name
         ,hr_organ_id
         ,hr_organ_name
         -- ,hr_comm_id
         -- ,hr_comm_name
         -- ,comm_name
    FROM dim_organ_mapping
    WHERE length(hr_comm_id) > 0
    GROUP BY
        organ_code
           ,organ_name
           ,hr_organ_id
           ,hr_organ_name
         --  ,hr_comm_id
         --  ,hr_comm_name
         --  ,comm_name
    UNION ALL
    SELECT
        '0000' AS organ_code    -- erp片区编码
         ,''    AS organ_name  -- erp片区名称
         ,'153'    AS hr_organ_id  -- hr片区编码
         ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
       --  ,'153'    AS hr_comm_id    -- hr项目id
       --  ,'领悦集团总部'    AS hr_comm_name  -- hr项目名称
       --  ,''     AS comm_name
)B
ON A.pq = B.hr_organ_id
-- and A.xm = b.hr_comm_id
where 1 = 1
${IF(LEN(p_area)>0," AND B.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
-- ${IF(LEN(p_shop)!=0," and B.hr_comm_name IN ('"+JOINARRAY(p_shop,"','")+"')","")}

group by
yd
order by
CASE WHEN yd = '01' THEN 'a'
     WHEN yd = '02' THEN 'b'
     WHEN yd = '03' THEN 'c'
     WHEN yd = '04' THEN 'd'
     WHEN yd = '05' THEN 'e'
     WHEN yd = '06' THEN 'f'
     WHEN yd = '07' THEN 'g'
     WHEN yd = '08' THEN 'h'
     WHEN yd = '09' THEN 'i'
     WHEN yd = '10' THEN 'j'
     WHEN yd = '11' THEN 'k'
     WHEN yd = '12' THEN 'l' END


-- ======================================
-- 员工满编率排名
-- ======================================
-- 片区排名

${IF(LEN(p_area)=0,"", "/*")}
SELECT
T1.organ_comm AS organ_comm
,ROW_NUMBER() OVER(ORDER BY T1.RATE DESC) 排名
,T1.rate
,T1.full_rate_target AS target_rate
FROM
    (
        SELECT
            B.hr_organ_name AS organ_comm
             ,CASE WHEN SUM(bzs) = 0 THEN 0 ELSE SUM(A.zzs) / SUM(A.bzs) END AS RATE
             ,SUM(C.full_rate_target) AS full_rate_target
        FROM
        (
                SELECT
                A1.pq AS pq
                ,SUM(A1.zzs) AS zzs
                ,SUM(A1.bzs) AS bzs
                FROM
                ( SELECT
                      pq	-- 片区
                     ,ny
                     ,SUM(zzs) AS zzs 	-- 在职数
                     ,SUM(bzs) AS bzs	-- 编制数
                    FROM ods_hr_view_mbl_d
                    WHERE pq NOT IN ('89','267') -- 这里剔除了佳美物业公司/悦汇发展公司
                    and ny > '2024-10'
                    GROUP BY pq,ny
                    UNION ALL
                    SELECT
                    organ_code as pq
     ,concat(left(ym,4),'-',right(ym,2)) as ny
     ,SUM(zzs) AS zzs
    ,SUM(bzs) AS bzs
     FROM ods_hr_history_bz_num
     WHERE 1 = 1
     GROUP BY organ_code,concat(left(ym,4),'-',right(ym,2))
            )A1
            WHERE A1.ny = '${p_ym}'
            group by A1.pq
            )A
            LEFT JOIN
            (
                SELECT
                    organ_code
                     ,organ_name
                     ,hr_organ_id
                     ,hr_organ_name
                FROM dim_organ_mapping
                WHERE length(hr_organ_id) > 0
                GROUP BY
                    organ_code
                       ,organ_name
                       ,hr_organ_id
                       ,hr_organ_name

                UNION ALL
                SELECT
                    '0000' AS organ_code    -- erp片区编码
                     ,''    AS organ_name  -- erp片区名称
                     ,'153'    AS hr_organ_id  -- hr片区编码
                     ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
            )B
            ON A.pq = B.hr_organ_id
            LEFT JOIN
            (
                SELECT
                organ_code
                ,full_rate_target -- 满编率目标
                FROM ods_hr_collection_target_organ
                WHERE ym = '${p_ym}'
            )C
             ON B.organ_code = C.organ_code
        where 1 = 1
        ${IF(LEN(p_area)>0," AND B.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
        GROUP BY
        B.hr_organ_name
    )T1
${IF(LEN(p_area)=0,"", "*/")}

-- 项目排名
${IF(LEN(p_area) > 0,"", "/*")}
SELECT
    T1.organ_comm AS organ_comm
     ,ROW_NUMBER() OVER(ORDER BY T1.RATE DESC) 排名
     ,T1.rate
     ,T1.full_rate_target AS target_rate
FROM
(
        SELECT
            B.hr_comm_name AS organ_comm
             ,CASE WHEN SUM(bzs) = 0 THEN 0 ELSE SUM(A.zzs) / SUM(A.bzs) END AS RATE
        ,SUM(C.full_rate_target) AS full_rate_target
        FROM
        (
            SELECT
                A1.pq AS pq
                 ,A1.xm AS xm
                 ,A1.zzs AS zzs
                 ,A1.bzs AS bzs
            FROM
                (
                    SELECT
                        pq	-- 片区
                         ,xm
                         ,ny
                         ,SUM(CASE WHEN ny >= '2024-10' THEN zzs ELSE 0 end) AS zzs 	-- 在职数
                         ,SUM(bzs) AS bzs	-- 编制数
                    FROM ods_hr_view_mbl_d
                    WHERE pq NOT IN ('89','267') -- 这里剔除了佳美物业公司/悦汇发展公司
                    GROUP BY pq,ny,xm
                    UNION ALL
                    SELECT
                        T1.organ_code  AS pq
                        ,T1.comm_id   AS xm
                         ,T1.ym            AS ny
                         ,SUM(T1.begin_num  ) AS zzs-- 月初在职
                         ,0 AS bzs
                    FROM
                        (
                            SELECT
                                organ_code
                                 ,organ_name
                                 ,comm_id
                                 ,comm_name
                                 ,'2024-01' AS ym
                                 ,COALESCE(jan_begin_num,0)      AS begin_num -- 月初在职
                            FROM ods_hr_history_erp_date
                            UNION ALL
                            SELECT
                                organ_code
                                 ,organ_name
                                 ,comm_id
                                 ,comm_name
                                 ,'2024-02' AS ym
                                 ,COALESCE(feb_begin_num,0)      AS begin_num -- 月初在职
                            FROM ods_hr_history_erp_date
                            UNION ALL
                            SELECT
                                organ_code
                                 ,organ_name
                                 ,comm_id
                                 ,comm_name
                                 ,'2024-03' AS ym
                                 ,COALESCE(mar_begin_num,0)      AS begin_num -- 月初在职
                            FROM ods_hr_history_erp_date
                            UNION ALL
                            SELECT
                                organ_code
                                 ,organ_name
                                 ,comm_id
                                 ,comm_name
                                 ,'2024-04' AS ym
                                 ,COALESCE(apr_begin_num,0)      AS begin_num -- 月初在职
                            FROM ods_hr_history_erp_date
                            UNION ALL
                            SELECT
                                organ_code
                                 ,organ_name
                                 ,comm_id
                                 ,comm_name
                                 ,'2024-05' AS ym
                                 ,COALESCE(may_begin_num,0)      AS begin_num -- 月初在职
                            FROM ods_hr_history_erp_date
                            UNION ALL
                            SELECT
                                organ_code
                                 ,organ_name
                                 ,comm_id
                                 ,comm_name
                                 ,'2024-06' AS ym
                                 ,COALESCE(jun_begin_num,0)      AS begin_num -- 月初在职
                            FROM ods_hr_history_erp_date
                            UNION ALL
                            SELECT
                                organ_code
                                 ,organ_name
                                 ,comm_id
                                 ,comm_name
                                 ,'2024-07' AS ym
                                 ,COALESCE(jul_begin_num,0)      AS begin_num -- 月初在职
                            FROM ods_hr_history_erp_date
                            UNION ALL
                            SELECT
                                organ_code
                                 ,organ_name
                                 ,comm_id
                                 ,comm_name
                                 ,'2024-08' AS ym
                                 ,COALESCE(aug_begin_num,0)      AS begin_num -- 月初在职
                            FROM ods_hr_history_erp_date
                            UNION ALL
                            SELECT
                                organ_code
                                 ,organ_name
                                 ,comm_id
                                 ,comm_name
                                 ,'2024-09' AS ym
                                 ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                            FROM ods_hr_history_erp_date
                        )T1
                    GROUP BY  T1.ym,T1.organ_code,T1.comm_id
                )A1
            WHERE A1.ny = '${p_ym}'
        )A
        LEFT JOIN
        (
            SELECT
                organ_code
                 ,organ_name
                 ,hr_organ_id
                 ,hr_organ_name
                 ,hr_comm_id
                 ,hr_comm_name
                 ,comm_name
            FROM dim_organ_mapping
            WHERE length(hr_comm_id) > 0
            GROUP BY
                organ_code
                   ,organ_name
                   ,hr_organ_id
                   ,hr_organ_name
                   ,hr_comm_id
                   ,hr_comm_name
                   ,comm_name
            UNION ALL
            SELECT
                '0000' AS organ_code    -- erp片区编码
                 ,''    AS organ_name  -- erp片区名称
                 ,'153'    AS hr_organ_id  -- hr片区编码
                 ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
                 ,'153'    AS hr_comm_id    -- hr项目id
                 ,'领悦集团总部'    AS hr_comm_name  -- hr项目名称
                 ,''     AS comm_name
            )B
            ON A.pq = B.hr_organ_id
            and A.xm = B.hr_comm_id
            LEFT JOIN
            (
               SELECT
                comm_id
               ,organ_code
               ,full_rate_target -- 满编率目标
               FROM ods_hr_collection_target
               WHERE ym = '${p_ym}'
            )C
            ON B.organ_code = C.organ_code
            AND B.hr_comm_id = C.comm_id
            WHERE 1=1
            ${IF(LEN(p_area)>0," AND B.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
${IF(LEN(p_shop)!=0," and B.hr_comm_name IN ('"+JOINARRAY(p_shop,"','")+"')","")}
        GROUP BY
        B.hr_comm_name
)T1

${IF(LEN(p_area)>0,"", "*/")}


-- ======================================
-- 员工离职原因占比
-- ======================================
SELECT
A.lzlx	        AS lzlx     -- 离职类型id
,A.lzlxname     AS lzlxname -- 离职类型名称
,A.lzyy	        AS lzyy     -- 离职原因id
,A.lzyyname     AS lzyyname -- 离职原因名称
,A.lzrs	        AS lzrs     -- 离职人数
FROM
(
    SELECT
     pq	-- 片区
     ,xm	-- 项目
     ,lzlx	-- 离职类型id
     ,lzlxname -- 离职类型名称
     ,lzyy	-- 离职原因id
     ,lzyyname	-- 离职原因名称
     ,lzrs	-- 离职人数
    FROM  ods_hr_view_lzrs_fl_d
    WHERE LEFT(rq,7) = '${p_ym}'
)A
LEFT JOIN
(
    SELECT
        organ_code
         ,organ_name
         ,hr_organ_id
         ,hr_organ_name
         ,hr_comm_id
         ,hr_comm_name
         ,comm_name
    FROM dim_organ_mapping
    WHERE length(hr_comm_id) > 0
    GROUP BY
        organ_code
           ,organ_name
           ,hr_organ_id
           ,hr_organ_name
           ,hr_comm_id
           ,hr_comm_name
           ,comm_name
    UNION ALL
    SELECT
        '0000' AS organ_code    -- erp片区编码
         ,''    AS organ_name  -- erp片区名称
         ,'153'    AS hr_organ_id  -- hr片区编码
         ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
         ,'153'    AS hr_comm_id    -- hr项目id
         ,'领悦集团总部'    AS hr_comm_name  -- hr项目名称
         ,''     AS comm_name
)B
ON A.pq = B.hr_organ_id
and A.xm = B.hr_comm_id
where 1 = 1
${IF(LEN(p_area)>0," AND B.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
${IF(LEN(p_shop)!=0," and B.hr_comm_name IN ('"+JOINARRAY(p_shop,"','")+"')","")}

-- ======================================
-- 离职率排名
-- ======================================

-- 片区
${IF(LEN(p_area)=0,"", "/*")}
SELECT
    T.hr_organ_name AS hr_organ_name
     ,T.lz_rate AS lz_rate
     ,T.lz_rate_target   AS lz_rate_target
     ,ROW_NUMBER() OVER(ORDER BY T.lz_rate desc) AS 排名
FROM
    (
        SELECT
            B.hr_organ_name AS hr_organ_name
         ,CASE WHEN sum(A.zzrs) = 0 THEN 0
                   ELSE  SUM(A.zlzrs) /  sum(zzrs)
            END AS lz_rate  -- 离职率
        ,SUM(C.lz_rate_target) AS lz_rate_target
        FROM
        (

        SELECT
        T.pq as pq
        ,SUM(T.zzrs) AS zzrs
        ,SUM(T.zlzrs) AS zlzrs
        FROM
        (
            SELECT
                pq	 -- 片区
                ,LEFT(rq,7) AS ym
                 ,(SUM(CASE WHEN rq = CONCAT('${p_ym}','-01') THEN zzrs ELSE 0 END)
                + SUM(CASE WHEN rq = LAST_DAY(CONCAT('${p_ym}','-01')) THEN zzrs ELSE 0 END)) / 2
                AS zzrs	-- 当天在职人数
                ,SUM(zlzrs)	 AS zlzrs   -- 当天总离职人数
                FROM ods_hr_view_lzbl_d
                WHERE LEFT(rq, 7) >= '2024-10'
                -- 排除了:
                -- 89:佳美物业公司
                -- 267:悦汇发展公司
                GROUP BY pq,LEFT(rq,7)
                UNION ALL
                SELECT
                T1.organ_code  AS pq
                 ,T1.ym          AS ym
                 ,(SUM(T1.begin_num  ) + SUM(T1.end_num)) / 2 AS zzrs-- 在职人数
                 ,SUM(T1.active_quit) + SUM(T1.pass_quit ) AS zlzrs
            FROM
                (
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-01' AS ym
                         ,COALESCE(jan_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(jan_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(jan_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-02' AS ym
                         ,COALESCE(feb_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(feb_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(feb_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(feb_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-03' AS ym
                         ,COALESCE(mar_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(mar_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(mar_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(mar_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-04' AS ym
                         ,COALESCE(apr_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(apr_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(apr_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(apr_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-05' AS ym
                         ,COALESCE(may_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(may_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(may_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(may_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-06' AS ym
                         ,COALESCE(jun_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(jun_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(jun_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(jun_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-07' AS ym
                         ,COALESCE(jul_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(jul_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(jul_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(jul_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-08' AS ym
                         ,COALESCE(aug_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(aug_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(aug_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(aug_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-09' AS ym
                         ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(sep_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(sep_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(sep_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                )T1
                GROUP BY T1.organ_code ,T1.ym
            )T
            WHERE LEFT(ym,7) = '${p_ym}'
            AND pq not in('89','267') -- 排除了佳美、悦汇
            group by  T.pq
        )A
        RIGHT JOIN
        (
                SELECT
                    organ_code
                     ,organ_name
                     ,hr_organ_id
                     ,hr_organ_name
                FROM dim_organ_mapping
                WHERE length(hr_organ_id) > 0
                GROUP BY
                    organ_code
                       ,organ_name
                       ,hr_organ_id
                       ,hr_organ_name
                UNION ALL
                SELECT
                    '0000' AS organ_code    -- erp片区编码
                     ,''    AS organ_name  -- erp片区名称
                     ,'153'    AS hr_organ_id  -- hr片区编码
                     ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
            )B
            ON A.pq = B.hr_organ_id
            LEFT JOIN
            (
                SELECT
                organ_code
                 ,lz_rate_target -- 离职率目标
                FROM ods_hr_collection_target_organ
                WHERE ym = '${p_ym}'
            )C
        ON B.organ_code = C.organ_code
        where 1 = 1
        ${IF(LEN(p_area)>0," AND B.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}

        GROUP BY
            B.hr_organ_name
    )T
${IF(LEN(p_area)=0,"", "*/")}


${IF(LEN(p_area)>0,"", "/*")}
-- 项目
SELECT
    T.hr_organ_name      AS hr_organ_name
     ,T.lz_rate          AS lz_rate -- 离职率
     ,T.lz_rate_target   AS lz_rate_target -- 离职率目标
     ,ROW_NUMBER() OVER(ORDER BY T.lz_rate desc) AS 排名
FROM
    (
        SELECT
             B.hr_comm_name AS hr_organ_name
             ,CASE WHEN SUM(A.zzrs) = 0 THEN 0
                   ELSE SUM(A.zlzrs) /  SUM(A.zzrs)
            END AS lz_rate
        ,SUM(C.lz_rate_target) AS lz_rate_target

        FROM
        (
          SELECT
        T.pq as pq
        ,T.xm AS xm
        ,SUM(T.zzrs) AS zzrs
        ,sum(T.zlzrs) AS zlzrs
        FROM
        (
            SELECT
                pq	 -- 片区
                ,xm	 -- 项目
                ,LEFT(rq,7) AS ym
                ,(SUM(CASE WHEN rq = CONCAT('${p_ym}','-01') THEN zzrs ELSE 0 END)
                + SUM(CASE WHEN rq = LAST_DAY(CONCAT('${p_ym}','-01')) THEN zzrs ELSE 0 END)) / 2
                AS zzrs	-- 当天在职人数
                ,SUM(zlzrs)	 AS zlzrs   -- 当天总离职人数
                FROM ods_hr_view_lzbl_d
                WHERE LEFT(rq, 7) >= '2024-10'
                GROUP BY pq, xm, rq
                UNION ALL

            SELECT
                T1.organ_code  AS pq
                 ,T1.comm_id     AS xm
                 ,T1.ym          AS ym
                 ,(SUM(T1.begin_num  ) + SUM(T1.end_num)) / 2 AS zzrs-- 在职人数
                 ,SUM(T1.active_quit) + SUM(T1.pass_quit ) AS zlzrs
            FROM
                (
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-01' AS ym
                         ,COALESCE(jan_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(jan_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(jan_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(jan_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-02' AS ym
                         ,COALESCE(feb_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(feb_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(feb_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(feb_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-03' AS ym
                         ,COALESCE(mar_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(mar_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(mar_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(mar_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-04' AS ym
                         ,COALESCE(apr_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(apr_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(apr_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(apr_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-05' AS ym
                         ,COALESCE(may_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(may_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(may_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(may_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-06' AS ym
                         ,COALESCE(jun_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(jun_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(jun_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(jun_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-07' AS ym
                         ,COALESCE(jul_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(jul_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(jul_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(jul_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-08' AS ym
                         ,COALESCE(aug_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(aug_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(aug_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(aug_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                    UNION ALL
                    SELECT
                        organ_code
                         ,organ_name
                         ,comm_id
                         ,comm_name
                         ,'2024-09' AS ym
                         ,COALESCE(sep_begin_num,0)      AS begin_num -- 月初在职
                         ,COALESCE(sep_end_num,0)        AS end_num   -- 月末在职
                         ,COALESCE(sep_active_quit,0)    AS active_quit -- 主动离职人数
                         ,COALESCE(sep_pass_quit,0)      AS pass_quit  -- 被动离职人数
                    FROM ods_hr_history_erp_date
                )T1
                GROUP BY T1.organ_code, T1.comm_id ,T1.ym
            )T
 WHERE  LEFT(ym,7) = '${p_ym}'
            AND pq not in('89','267') -- 排除了佳美、悦汇
    AND (pq is NOT NULL OR xm IS NOT NULL )
    group by T.pq,T.xm
        )A
        RIGHT JOIN
        ( SELECT
                organ_code
                 ,organ_name
                 ,hr_organ_id
                 ,hr_organ_name
                 ,hr_comm_id
                 ,hr_comm_name
                 ,comm_id
                 ,comm_name
            FROM dim_organ_mapping
            WHERE length(hr_comm_id) > 0
            GROUP BY
                organ_code
                   ,organ_name
                   ,hr_organ_id
                   ,hr_organ_name
                   ,hr_comm_id
                   ,hr_comm_name
                   ,comm_name
                   ,comm_id
            UNION ALL
            SELECT
                '0000' AS organ_code    -- erp片区编码
                 ,''    AS organ_name  -- erp片区名称
                 ,'153'    AS hr_organ_id  -- hr片区编码
                 ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
                 ,'153'    AS hr_comm_id    -- hr项目id
                 ,'领悦集团总部'    AS hr_comm_name  -- hr项目名称
                 ,''     AS comm_id
                 ,''     AS comm_name
        )B
        ON A.pq = B.hr_organ_id
        and A.xm = b.hr_comm_id
        LEFT JOIN
        (
            SELECT
                comm_id
                 ,organ_code
                 ,lz_rate_target -- 离职率目标
            FROM ods_hr_collection_target
            WHERE ym = '${p_ym}'
        )C
          ON B.organ_code = C.organ_code
            AND B.hr_comm_id = C.comm_id
        where 1 = 1
            ${IF(LEN(p_area)>0," AND B.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
       ${IF(LEN(p_shop)!=0," and B.hr_comm_name IN ('"+JOINARRAY(p_shop,"','")+"')","")}

        GROUP BY
       B.hr_comm_name
    )T
${IF(LEN(p_area)>0,"", "*/")}


-- ======================================
-- 人力行政成本费率柱状图
-- ======================================

${if(p_type='人力',"","/*")}
SELECT
    T1.yr_month AS yr_month
        ,CONCAT(right(T1.yr_month,2),'月') as m
     ,'人力成本' AS name
     ,'人力成本费率' AS name_rate
     ,T1.lc_total_fee AS total_fee -- 人力成本
     ,T1.index_income_fact AS index_income_fact  -- 实际收入
     ,T1.lc_total_fee / T1.index_income_fact AS fee_rate -- 人力成本费率
FROM
(
    SELECT
        A.ym                     AS yr_month
        , SUM(A.lc_total_fee)      AS lc_total_fee
        , SUM(B.index_income_fact) AS index_income_fact
    FROM
    (
        SELECT
          A.ym              AS ym
         ,A.organ_code      AS organ_code
         ,A1.comm_id         AS comm_id  -- erp项目 匹配财务收入填报
         ,A1.hr_comm_name       AS hr_comm_name -- erp项目，用作权限控制
         ,A.lc_total_fee     AS lc_total_fee
       FROM
       (
               -- 费控人力行政成本填报
               SELECT organ_code
                    , comm_id
                    , ym
                    , COALESCE(SUM(CASE WHEN cost_type = '人力成本' THEN actual_amount ELSE 0 END),
                               0)                                                                      AS lc_total_fee -- 人力成本费用
                    , COALESCE(SUM(CASE WHEN cost_type = '行政成本' THEN actual_amount ELSE 0 END),
                               0)                                                                      AS ac_total_fee -- 行政成本费用
               FROM dw.ods_cost_collection_lc_ac
               WHERE LEFT(ym, 4) = LEFT('${p_ym}', 4)
                 AND ym <= '${p_ym}'
               GROUP BY organ_code, ym, comm_id) A
               LEFT JOIN
           (SELECT organ_code
                 , organ_name
                 , comm_id
                 , comm_name
                 , hr_organ_id
                 , hr_organ_name
                 , hr_comm_id
                 , hr_comm_name
            FROM dim_organ_mapping
            WHERE LENGTH(hr_comm_id) > 0
            GROUP BY organ_code
                   , organ_name
                   , comm_id
                   , comm_name
                   , hr_organ_id
                   , hr_organ_name
                   , hr_comm_id
                   , hr_comm_name
            UNION ALL
            SELECT '0000'         AS organ_code    -- erp片区编码
                 , ''             AS organ_name    -- erp片区名称
                 , ''             AS comm_id
                 , ''             AS comm_name
                 , '153'          AS hr_organ_id   -- hr片区编码
                 , '领悦集团总部' AS hr_organ_name -- hr片区名称
                 , '153'          AS hr_comm_id    -- hr项目id
                 , '领悦集团总部' AS hr_comm_name -- hr项目名称
           ) A1
           ON A.organ_code = A1.organ_code
            AND A.comm_id = A1.hr_comm_id
        )A
    LEFT JOIN
    (-- 公司实际收入
        SELECT
         yr_month
        ,organ_code
        ,comm_id
        ,SUM(index_income_fact) AS index_income_fact -- 实际收入
        FROM ods_caiwu_feecollection
        WHERE category = '物业'
        AND LEFT(yr_month,4) =  left('${p_ym}',4)
        GROUP BY
        yr_month
        ,organ_code
        ,comm_id
    )B
    ON A.organ_code  = B.organ_code
    AND A.ym = B.yr_month
    AND A.comm_id = B.comm_id
    WHERE 1 = 1
    ${IF(LEN(p_area)>0," AND A.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_shop)!=0," and A.hr_comm_name IN ('"+JOINARRAY(p_shop,"','")+"')","")}
    GROUP BY A.ym
)T1
ORDER BY T1.yr_month

${if(p_type='人力',"","*/")}

${if(p_type='行政',"","/*")}
SELECT
    T1.yr_month AS yr_month
        ,CONCAT(right(T1.yr_month,2),'月') as m
     ,'行政成本' AS name
     ,'行政成本费率' AS name_rate
     ,T1.ac_total_fee AS total_fee -- 行政成本
     ,T1.index_income_fact AS index_income_fact  -- 实际收入
     ,T1.ac_total_fee / T1.index_income_fact AS fee_rate -- 人力成本费率
FROM
(
    SELECT
      A.ym                AS yr_month
     ,SUM(A.ac_total_fee)      AS ac_total_fee      -- 行政成本
     ,SUM(B.index_income_fact) AS index_income_fact -- 实际收入
    FROM
    (
        SELECT
        A.ym            AS ym
        ,A.organ_code   AS organ_code
        ,A1.comm_id      AS comm_id -- erp项目 匹配财务收入填报
        ,A1.hr_comm_name   AS hr_comm_name -- hr项目，用作权限控制
        ,A.ac_total_fee AS ac_total_fee
        FROM
        ( -- 费控人力行政成本填报
               SELECT organ_code
                    , ym
                    , comm_id
                    , COALESCE(SUM(CASE WHEN cost_type = '人力成本' THEN actual_amount ELSE 0 END),
                               0)                                                                      AS lc_total_fee -- 人力成本费用
                    , COALESCE(SUM(CASE WHEN cost_type = '行政成本' THEN actual_amount ELSE 0 END),
                               0)                                                                      AS ac_total_fee -- 行政成本费用
               FROM dw.ods_cost_collection_lc_ac
               WHERE LEFT(ym, 4) = LEFT('${p_ym}', 4)
                 AND ym <= '${p_ym}'
               GROUP BY organ_code, ym, comm_id
        ) A
        LEFT JOIN
        (
            SELECT organ_code
                 , organ_name
                 , comm_id
                 , comm_name
                 , hr_organ_id
                 , hr_organ_name
                 , hr_comm_id
                 , hr_comm_name
            FROM dim_organ_mapping
            WHERE LENGTH(hr_comm_id) > 0
            GROUP BY organ_code
                   , organ_name
                   , comm_id
                   , comm_name
                   , hr_organ_id
                   , hr_organ_name
                   , hr_comm_id
                   , hr_comm_name
            UNION ALL
            SELECT '0000'         AS organ_code    -- erp片区编码
                 , ''             AS organ_name    -- erp片区名称
                 , ''             AS comm_id
                 , ''             AS comm_name
                 , '153'          AS hr_organ_id   -- hr片区编码
                 , '领悦集团总部' AS hr_organ_name -- hr片区名称
                 , '153'          AS hr_comm_id    -- hr项目id
                 , '领悦集团总部' AS hr_comm_name -- hr项目名称
           ) A1
           ON A.organ_code = A1.organ_code
           AND A.comm_id = A1.hr_comm_id
    )A
    LEFT JOIN
    (-- 公司实际收入
        SELECT
            yr_month
             ,organ_code
             ,comm_id
             ,SUM(index_income_fact) AS index_income_fact -- 实际收入
        FROM ods_caiwu_feecollection
        WHERE category = '物业'
       AND LEFT(yr_month,4) =  left('${p_ym}',4)
        GROUP BY
        yr_month
        ,organ_code
        ,comm_id
    )B
    ON A.ym = B.yr_month
    AND A.organ_code  = B.organ_code
    AND A.comm_id = B.comm_id
    WHERE 1=1
    ${IF(LEN(p_area)>0," AND A.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_shop)!=0," and A.hr_comm_name IN ('"+JOINARRAY(p_shop,"','")+"')","")}
    GROUP BY A.ym
)T1
ORDER BY T1.yr_month
${if(p_type='行政',"","*/")}


-- ======================================
-- 人力行政成本费率排名
-- ======================================

-- 片区排名

${if(and(len(p_area)=0,p_type='人力')," -- 片区排名开始", "/*")}
SELECT
       A.organ_name   AS  organ_comm_name
      ,A.lc_total_fee        AS actual_cost  -- 人力成本
      ,B.index_income_fact  AS index_income_fact -- 实际收入
      ,ROW_NUMBER() OVER (ORDER BY  A.lc_total_fee / B.index_income_fact DESC ) AS 排名 -- 费率排名
      ,A.lc_total_fee /  B.index_income_fact AS fee_rate      -- 人力成本费率
      ,C.lc_fee_rate_target  AS target_fee_rate -- 费率目标
    FROM
    (    -- 费控人力行政成本填报
        SELECT
            organ_code
             ,organ_name
             ,coalesce(SUM(CASE WHEN cost_type = '人力成本' THEN actual_amount ELSE 0 END),0) AS lc_total_fee -- 人力成本费用
             ,coalesce(SUM(CASE WHEN cost_type = '行政成本' THEN actual_amount ELSE 0 END),0) AS ac_total_fee  -- 行政成本费用
        from dw.ods_cost_collection_lc_ac
        WHERE  left(ym,4) =  left('${p_ym}',4)
          AND ym <= '${p_ym}'
        ${IF(LEN(p_area)>0," AND A.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
        GROUP BY organ_code,organ_name
    )A
    LEFT JOIN
    (-- 公司实际收入
        SELECT
          organ_code
         ,SUM(index_income_fact) AS index_income_fact -- 实际收入
         ,SUM(index_income_target) AS index_income_target -- 收入目标
        FROM ods_caiwu_feecollection
        WHERE category = '物业'
          AND yr_month = '${p_ym}'
        GROUP BY
         organ_code
    )B
    ON A.organ_code = B.organ_code
    LEFT JOIN
    (
        SELECT
            organ_code
             ,lc_fee_rate_target    -- 人力成本费率目标
             ,ac_fee_rate_target    -- 行政成本费率目标
        FROM ods_hr_collection_target_organ
        WHERE ym = '${p_ym}'
    )C
    ON A.organ_code = C.organ_code

${if(and(len(p_area)=0,p_type='人力')," -- 片区排名开始", "*/")}


${if(and(len(p_area)=0,p_type='行政')," -- 片区排名开始", "/*")}

SELECT
    A.organ_name   AS  organ_comm_name
     ,A.ac_total_fee        AS actual_cost  -- 成本
     ,B.index_income_fact  AS index_income_fact -- 实际收入
     ,ROW_NUMBER() OVER (ORDER BY  A.ac_total_fee / B.index_income_fact DESC ) AS 排名 -- 费率排名
     ,A.ac_total_fee /  B.index_income_fact AS fee_rate      -- 行政成本费率
     ,C.ac_fee_rate_target   AS target_fee_rate -- 费率目标
    FROM
    (        -- 费控人力行政成本填报
        SELECT
            organ_code
             ,organ_name
             ,coalesce(SUM(CASE WHEN cost_type = '人力成本' THEN actual_amount ELSE 0 END),0) AS lc_total_fee -- 人力成本费用
             ,coalesce(SUM(CASE WHEN cost_type = '行政成本' THEN actual_amount ELSE 0 END),0) AS ac_total_fee  -- 行政成本费用
        from dw.ods_cost_collection_lc_ac
        WHERE  left(ym,4) =  left('${p_ym}',4)
        AND ym <= '${p_ym}'
        ${IF(LEN(p_area)>0," AND A.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
        GROUP BY organ_code,organ_name
    )A
    LEFT JOIN
    (-- 公司实际收入
        SELECT
             organ_code
             ,SUM(index_income_fact) AS index_income_fact -- 实际收入
        FROM ods_caiwu_feecollection
        WHERE category = '物业'
        AND yr_month = '${p_ym}'
        GROUP BY
        organ_code
    )B
    ON A.organ_code  = B.organ_code
    LEFT JOIN
    (
        SELECT
            organ_code
             ,lc_fee_rate_target    -- 人力成本费率目标
             ,ac_fee_rate_target    -- 行政成本费率目标
        FROM ods_hr_collection_target_organ
        WHERE ym = '${p_ym}'
    )C
    ON A.organ_code = C.organ_code
${if(and(len(p_area)=0,p_type='行政')," -- 片区排名开始", "*/")}



-- 项目排名

${if(and(len(p_area)>0,p_type='人力')," -- 片区排名开始", "/*")}
SELECT
    T.organ_comm_name AS organ_comm_name
     ,T.actual_cost AS actual_cost
     ,T.index_income_fact AS index_income_fact -- 实际收入
     ,ROW_NUMBER() OVER (ORDER BY T.fee_rate DESC ) AS 排名 -- 费率排名
     ,T.fee_rate AS fee_rate
     ,T. target_fee_rate AS target_fee_rate -- 费率目标
FROM
(
    SELECT
        A.comm_name             AS  organ_comm_name
      ,SUM(A.lc_total_fee)   AS actual_cost  -- 成本
      ,SUM(B.index_income_fact)  AS index_income_fact -- 实际收入
      ,CASE WHEN sum(B.index_income_fact) = 0 THEN 0
            ELSE SUM(A.lc_total_fee) / SUM(B.index_income_fact) END AS fee_rate
      ,SUM(C.lc_fee_rate_target)   AS target_fee_rate -- 费率目标
    FROM
    (   SELECT
        A1.organ_code   AS organ_code
        ,A1.comm_id     AS comm_id
        ,A1.comm_name   AS comm_name
        ,A1.lc_total_fee AS lc_total_fee
        ,A2.comm_id     AS erp_comm_id
        FROM
        (
             -- 费控人力行政成本填报
             SELECT organ_code
                  , comm_id
                  , comm_name
                  , COALESCE(SUM(CASE WHEN cost_type = '人力成本' THEN actual_amount ELSE 0 END),
                             0)                                                                      AS lc_total_fee -- 人力成本费用
                  , COALESCE(SUM(CASE WHEN cost_type = '行政成本' THEN actual_amount ELSE 0 END),
                             0)                                                                      AS ac_total_fee -- 行政成本费用
             FROM dw.ods_cost_collection_lc_ac
             WHERE LEFT(ym, 4) = LEFT('${p_ym}', 4)
               AND ym <= '${p_ym}'
             GROUP BY organ_code, comm_id, comm_name
        )A1
        LEFT JOIN
        (
            SELECT
                organ_code
                 ,organ_name
                 ,comm_id
                 ,comm_name
                 ,hr_organ_id
                 ,hr_organ_name
                 ,hr_comm_id
                 ,hr_comm_name
            FROM dim_organ_mapping
            WHERE length(hr_comm_id) > 0
            GROUP BY
                organ_code
                   ,organ_name
                   ,comm_id
                   ,comm_name
                   ,hr_organ_id
                   ,hr_organ_name
                   ,hr_comm_id
                   ,hr_comm_name
            UNION ALL
            SELECT
                '0000' AS organ_code    -- erp片区编码
                 ,''    AS organ_name  -- erp片区名称
                 ,''    AS comm_id
                 ,''    AS comm_name
                 ,'153'    AS hr_organ_id  -- hr片区编码
                 ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
                 ,'153'    AS hr_comm_id    -- hr项目id
                 ,'领悦集团总部'    AS hr_comm_name  -- hr项目名称
        )A2
        ON A1.organ_code = A2.organ_code
        and A1.comm_id = A2.hr_comm_id
        where 1 = 1
        ${IF(LEN(p_area)>0," AND A1.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
${IF(LEN(p_shop)!=0," and A2.hr_comm_name IN ('"+JOINARRAY(p_shop,"','")+"')","")}

    )A
    LEFT JOIN
    (-- 公司实际收入
        SELECT
        organ_code
        ,comm_id
        ,SUM(index_income_fact) AS index_income_fact -- 实际收入
        FROM ods_caiwu_feecollection
        WHERE category = '物业'
         AND yr_month = '${p_ym}'
        GROUP BY
        organ_code,comm_id
    )B
    ON A.organ_code  = B.organ_code
    AND A.erp_comm_id = B.comm_id
    LEFT JOIN
    (
        SELECT
            organ_code
            ,comm_id
            ,lc_fee_rate_target    -- 人力成本费率目标
            ,ac_fee_rate_target    -- 行政成本费率目标
        FROM ods_cost_collection_target
        WHERE ym = '${p_ym}'
    )C
    ON A.organ_code = C.organ_code
    AND A.comm_id = C.comm_id
    GROUP BY A.comm_name
)T
 ${if(and(len(p_area)>0,p_type='人力')," -- 片区排名开始", "*/")}


${if(and(len(p_area)>0,p_type='行政')," -- 片区排名开始", "/*")}

SELECT
T.organ_comm_name AS organ_comm_name
,T.actual_cost AS actual_cost
,T.index_income_fact AS index_income_fact -- 实际收入
,ROW_NUMBER() OVER (ORDER BY T.fee_rate DESC ) AS 排名 -- 费率排名
,T.fee_rate AS fee_rate
,T.target_fee_rate AS target_fee_rate -- 费率目标
FROM
(
    SELECT
    A.comm_name             AS  organ_comm_name
     ,SUM(A.ac_total_fee)   AS actual_cost  -- 成本
     ,SUM(B.index_income_fact)  AS index_income_fact -- 实际收入
  --   ,ROW_NUMBER() OVER (ORDER BY  A.lc_total_fee / B.index_income_fact DESC ) AS 排名 -- 费率排名
    ,CASE WHEN SUM(B.index_income_fact) = 0 THEN 0
          ELSE SUM(A.ac_total_fee) / SUM(B.index_income_fact) END AS fee_rate
     ,SUM(C.ac_fee_rate_target)   AS target_fee_rate -- 费率目标
    FROM
    (        -- 费控人力行政成本填报
        SELECT
            A1.organ_code   AS organ_code
             ,A1.comm_id     AS comm_id
             ,A1.comm_name   AS comm_name
             ,A1.ac_total_fee AS ac_total_fee
             ,A2.comm_id     AS erp_comm_id
        FROM
            (
                -- 费控人力行政成本填报
                SELECT organ_code
                     , comm_id
                     , comm_name
                     , COALESCE(SUM(CASE WHEN cost_type = '人力成本' THEN actual_amount ELSE 0 END),
                                0)                                                                      AS lc_total_fee -- 人力成本费用
                     , COALESCE(SUM(CASE WHEN cost_type = '行政成本' THEN actual_amount ELSE 0 END),
                                0)                                                                      AS ac_total_fee -- 行政成本费用
                FROM dw.ods_cost_collection_lc_ac
                WHERE LEFT(ym, 4) = LEFT('${p_ym}', 4)
                  AND ym <= '${p_ym}'
                GROUP BY organ_code, comm_id, comm_name
            )A1
                LEFT JOIN
            (
                SELECT
                    organ_code
                     ,organ_name
                     ,comm_id
                     ,comm_name
                     ,hr_organ_id
                     ,hr_organ_name
                     ,hr_comm_id
                     ,hr_comm_name
                FROM dim_organ_mapping
                WHERE length(hr_comm_id) > 0
                GROUP BY
                    organ_code
                       ,organ_name
                       ,comm_id
                       ,comm_name
                       ,hr_organ_id
                       ,hr_organ_name
                       ,hr_comm_id
                       ,hr_comm_name
                UNION ALL
                SELECT
                    '0000' AS organ_code    -- erp片区编码
                     ,''    AS organ_name  -- erp片区名称
                     ,''    AS comm_id
                     ,''    AS comm_name
                     ,'153'    AS hr_organ_id  -- hr片区编码
                     ,'领悦集团总部'    AS hr_organ_name  -- hr片区名称
                     ,'153'    AS hr_comm_id    -- hr项目id
                     ,'领悦集团总部'    AS hr_comm_name  -- hr项目名称
            )A2
            ON A1.organ_code = A2.organ_code
            and A1.comm_id = A2.hr_comm_id
        where 1 = 1
            ${IF(LEN(p_area)>0," AND A1.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
       ${IF(LEN(p_shop)!=0," and A2.hr_comm_name IN ('"+JOINARRAY(p_shop,"','")+"')","")}

    )A
    LEFT JOIN
    (-- 公司实际收入
        SELECT
            organ_code
             ,comm_id
             ,SUM(index_income_fact) AS index_income_fact -- 实际收入
        FROM ods_caiwu_feecollection
        WHERE category = '物业'
    AND yr_month = '${p_ym}'
        GROUP BY
            organ_code,comm_id
    )B
    ON A.organ_code  = B.organ_code
    AND A.erp_comm_id = B.comm_id
    LEFT JOIN
    (
        SELECT
            organ_code
             ,comm_id
             ,lc_fee_rate_target    -- 人力成本费率目标
             ,ac_fee_rate_target    -- 行政成本费率目标
        FROM ods_cost_collection_target
        WHERE ym = '${p_ym}'
    )C
    ON A.organ_code = C.organ_code
    AND A.comm_id = C.comm_id
    WHERE 1 = 1
     GROUP BY A.comm_name
    )T
${if(and(len(p_area)>0,p_type='行政')," -- 片区排名开始", "*/")}