
SELECT
    B.hr_comm_name  AS hr_comm_name -- 片区
    ,B.hr_organ_name  AS hr_organ_name  -- 项目
    ,SUM(A.bzs) AS bzs -- 编制数
    ,SUM(A.zzs) AS zzs -- 在职数
    ,SUM(C.full_rate_target) AS full_rate_target -- 满编率目标值
    ,SUM(C.avg_full_rate_target) AS avg_full_rate_target -- 月均满编率目标值
    ,CASE WHEN SUM(bzs) = 0 THEN 0 ELSE SUM(zzs) / SUM(bzs) END AS RATE -- 月均满编率
FROM
(
                SELECT
                    pq	-- 片区
                     ,xm	-- 项目
                     ,zzs	-- 在职数
                     ,bzs	-- 编制数
                FROM ods_hr_view_mbl_d
                WHERE ny >= '${p_startym}'
                AND ny <= '${p_ym}'
                -- 注意  目前会有一个空的排名 是因为佳美物业未做映射
                AND pq NOT IN('89','267')
            )A
            LEFT JOIN
            (
                SELECT
                organ_code    -- erp片区编码
                 ,organ_name  -- erp片区名称
                 ,hr_organ_id  -- hr片区编码
                 ,hr_organ_name  -- hr片区名称
                 ,hr_comm_id    -- hr项目id
                 ,hr_comm_name  -- hr项目名称
                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
                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项目名称
                FROM dual
            )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 -- 满编率目标
                ,avg_full_rate_target -- 月均满编率目标
                FROM ods_hr_collection_target
                WHERE ym = '${p_ym}'
            )C
        ON A.pq = C.organ_code
            AND A.xm = C.comm_id
            WHERE 1=1
            ${IF(LEN(p_area)>0," AND B.organ_code IN ('" + p_area +"')", "")}
        GROUP BY
            B.hr_comm_name
        ,B.hr_organ_name