
SELECT
T1.hr_comm_name AS hr_comm_name
,T1.hr_organ_name AS hr_organ_name
,T1.bzs           AS bzs -- 当月编制数
,T1.zzs           AS zzs -- 当月在职数
,'0'              AS target_full_rate  -- 月均满编率目标
,CASE when T1.bzs = 0 then 0 else T1.zzs / T1.bzs  end AS full_rate -- 月度满编率
,'0'              AS target_rate  -- 月均满编率目标
,T1.rate          AS rate -- 月均满编率实际
FROM
(
        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
             ,CASE WHEN SUM(avg_bzs) = 0 THEN 0 ELSE SUM(avg_zzs) / SUM(avg_bzs) END AS RATE
        FROM
        (
                SELECT
                    pq	-- 片区
                     ,xm	-- 项目
                     ,zzs	-- 在职数
                     ,bzs	-- 编制数
                     ,rq
                     ,datediff(current_date(),rq) AS datedif  -- 结束日期--开始日期
                     ,sum(zzs) over(PARTITION BY rq,pq,xm) AS total_zzs -- 累计在职人数
                     ,sum(bzs) over(PARTITION BY rq,pq,xm) AS total_bzs -- 累计编制人数
                     ,sum(zzs) over(PARTITION BY rq,pq,xm)  / datediff(current_date(),rq) AS avg_zzs -- 平均在职人数
                     ,sum(bzs) over(PARTITION BY rq,pq,xm)  / datediff(current_date(),rq) AS avg_bzs -- 平均编制人数
                FROM ods_hr_view_mbl_d
                WHERE CONCAT(nd,'-',yd) = '${p_ym}'
                -- 注意  目前会有一个空的排名 是因为佳美物业未做映射
                AND pq <> '89'
            )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_kunan_tmp
                WHERE hr_organ_name IS NOT NULL
                GROUP BY
                organ_code
                   ,organ_name
                   ,hr_organ_id
                   ,hr_organ_name
                   ,hr_comm_id
                   ,hr_comm_name
            )B
            ON A.pq = B.hr_organ_id
            and A.xm = B.hr_comm_id
            ${IF(LEN(p_area)>0," AND B.organ_code IN ('" + p_area +"')", "")}
        GROUP BY
            B.hr_comm_name
        ,B.hr_organ_name
)T1