-- ======================================
-- 年龄分布
-- ======================================

SELECT
nld
,sum(sl) AS sl
FROM
(
SELECT
zb	    -- 总部
,pq	    -- 片区
,xm	    -- 项目
,nld	--	年龄段
,sl	    --	数量
FROM ods_hr_view_nld_d
)A
LEFT JOIN
(
    SELECT
        organ_code
         ,hr_organ_id
         ,hr_comm_id
    FROM dim_organ_mapping
    GROUP BY
    organ_code
    ,hr_organ_id
    ,hr_comm_id
    UNION ALL
    SELECT
    '0000' AS organ_code    -- erp片区编码
    ,'153'    AS hr_organ_id  -- hr片区编码
    ,'153'    AS hr_comm_id    -- hr项目id
    FROM dual

)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,"','")+"')", "")}
GROUP BY nld




-- 年龄分布
-- 年龄＜30岁；30岁≤年龄＜40岁；40岁≤年龄＜50岁；50岁≤年龄＜60岁；60岁≤年龄＜65岁；年龄≥65岁；
with one_to_many AS(
SELECT
a.username AS username -- 用户名
,SUBSTRING_INDEX(SUBSTRING_INDEX(a.mang_project, ',',b.help_topic_id + 1),',',-1) AS mang_project -- 管理项目名
FROM ods_one_to_many_perm a
JOIN mysql.help_topic b
on b.help_topic_id < (LENGTH(a.mang_project) - length(replace(a.mang_project,',','')) + 1)
WHERE a.username  = '${fine_username}'
)
SELECT
    CASE WHEN A.nl < 30.00  THEN '年龄＜30岁'
         WHEN A.nl >= 30.00 AND A.nl < 40.00 THEN '30岁≤年龄＜40岁'
         WHEN A.nl >= 40.00 AND A.nl < 50.00 THEN '40岁≤年龄＜50岁'
         WHEN A.nl >= 50.00 AND A.nl < 60.00 THEN '50岁≤年龄＜60岁'
         WHEN A.nl >= 60.00 AND A.nl < 65.00 THEN '60岁≤年龄＜65岁'
         WHEN A.nl >= 65.00  THEN '年龄≥65岁'
        END AS nld
     ,count(1) AS sl
FROM
    (
        SELECT
            pq
             ,xm
             ,nl
        FROM ods_hr_view_zaizhi_user_d
        WHERE ny <= '${p_ym}'
        AND ny >=  '${p_startym}'
    )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
                ,'领悦集团总部'    AS hr_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 ('"+ p_area +"')", "")}
       AND CASE WHEN (SELECT mang_project FROM ods_one_to_many_perm WHERE username  = '${fine_username}' AND mang_organ IS NULL) IS NOT NULL
         THEN B.comm_name IN (SELECT mang_project FROM one_to_many WHERE username  = '${fine_username}')
          ELSE ${if(left(fine_role,2)=="项目","B.comm_name = '"+GETUSERDEPARTMENTS(3)+"'","1=1")} END
GROUP BY
    CASE WHEN nl < 30.00 THEN '年龄＜30岁'
    WHEN nl >= 30.00 AND nl < 40.00 THEN '30岁≤年龄＜40岁'
    WHEN nl >= 40.00 AND nl < 50.00 THEN '40岁≤年龄＜50岁'
    WHEN nl >= 50.00 AND nl < 60.00 THEN '50岁≤年龄＜60岁'
    WHEN nl >= 60.00 AND nl < 65.00 THEN '60岁≤年龄＜65岁'
    WHEN nl >= 65.00  THEN '年龄≥65岁'
    END


-- ======================================
-- 司龄分布
-- ======================================

SELECT
    sld
     ,sum(sl) AS sl
FROM
    (
        SELECT
            zb	    -- 总部
             ,pq	    -- 片区
             ,xm	    -- 项目
             ,sld	--	司龄段
             ,sl	    --	数量
        FROM ods_hr_view_sld_d
    )A
        LEFT JOIN
    (
        SELECT
            organ_code
             ,hr_organ_id
             ,hr_comm_id
        FROM dim_organ_mapping
        GROUP BY
            organ_code
               ,hr_organ_id
               ,hr_comm_id
        UNION ALL
        SELECT
            '0000' AS organ_code    -- erp片区编码
             ,'153'    AS hr_organ_id  -- hr片区编码
             ,'153'    AS hr_comm_id    -- hr项目id
        FROM dual

    )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,"','")+"')", "")}
GROUP BY sld

--   司龄＜3个月内；3个月≤司龄＜1年；1年≤司龄＜3年；3年≤司龄＜5年；5年≤司龄＜10年；10年≤司龄＜15年；司龄≥15年；
with one_to_many AS(
SELECT
a.username AS username -- 用户名
,SUBSTRING_INDEX(SUBSTRING_INDEX(a.mang_project, ',',b.help_topic_id + 1),',',-1) AS mang_project -- 管理项目名
FROM ods_one_to_many_perm a
JOIN mysql.help_topic b
on b.help_topic_id < (LENGTH(a.mang_project) - length(replace(a.mang_project,',','')) + 1)
WHERE a.username  = '${fine_username}'
)
SELECT
    CASE WHEN A.sl < 0.30  THEN '司龄＜3个月内'
         WHEN A.sl >= 0.30 AND A.sl < 1.00 THEN '3个月≤司龄＜1年'
         WHEN A.sl >= 1.00 AND A.sl < 3.00 THEN '1年≤司龄＜3年'
         WHEN A.sl >= 3.00 AND A.sl < 5.00 THEN '3年≤司龄＜5年'
         WHEN A.sl >= 5.00 AND A.sl < 10.00 THEN '5年≤司龄＜10年'
         WHEN A.sl >= 10.00 AND A.sl < 15.00 THEN '10年≤司龄＜15年'
         WHEN A.sl >= 15.00  THEN '司龄≥15年'
        END AS sld
     ,count(1) AS sl
FROM
    (
        SELECT
            pq
             ,xm
             ,sl
        FROM ods_hr_view_zaizhi_user_d
        WHERE ny <= '${p_ym}'
        AND ny >=  '${p_startym}'
    )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
                ,'领悦集团总部'    AS hr_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 ('"+ p_area +"')", "")}
          AND CASE WHEN (SELECT mang_project FROM ods_one_to_many_perm WHERE username  = '${fine_username}' AND mang_organ IS NULL) IS NOT NULL
         THEN B.comm_name IN (SELECT mang_project FROM one_to_many WHERE username  = '${fine_username}')
          ELSE ${if(left(fine_role,2)=="项目","B.comm_name = '"+GETUSERDEPARTMENTS(3)+"'","1=1")} END

GROUP BY
    CASE WHEN A.sl < 0.30  THEN '司龄＜3个月内'
    WHEN A.sl >= 0.30 AND A.sl < 1.00 THEN '3个月≤司龄＜1年'
    WHEN A.sl >= 1.00 AND A.sl < 3.00 THEN '1年≤司龄＜3年'
    WHEN A.sl >= 3.00 AND A.sl < 5.00 THEN '3年≤司龄＜5年'
    WHEN A.sl >= 5.00 AND A.sl < 10.00 THEN '5年≤司龄＜10年'
    WHEN A.sl >= 10.00 AND A.sl < 15.00 THEN '10年≤司龄＜15年'
    WHEN A.sl >= 15.00  THEN '司龄≥15年'
    END

-- ======================================
-- 学历分布
-- ======================================


SELECT
    xl
     ,sum(sl) AS sl
FROM
    (
        SELECT
            zb	    -- 总部
             ,pq	    -- 片区
             ,xm	    -- 项目
             ,xl	--	学历
             ,sl	    --	数量
        FROM ods_hr_view_xl_d
    )A
        LEFT JOIN
    (
        SELECT
            organ_code
             ,hr_organ_id
             ,hr_comm_id
        FROM dim_organ_mapping
        GROUP BY
            organ_code
               ,hr_organ_id
               ,hr_comm_id
        UNION ALL
        SELECT
            '0000' AS organ_code    -- erp片区编码
             ,'153'    AS hr_organ_id  -- hr片区编码
             ,'153'    AS hr_comm_id    -- hr项目id
        FROM dual

    )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,"','")+"')", "")}
GROUP BY xl



with one_to_many AS(
SELECT
a.username AS username -- 用户名
,SUBSTRING_INDEX(SUBSTRING_INDEX(a.mang_project, ',',b.help_topic_id + 1),',',-1) AS mang_project -- 管理项目名
FROM ods_one_to_many_perm a
JOIN mysql.help_topic b
on b.help_topic_id < (LENGTH(a.mang_project) - length(replace(a.mang_project,',','')) + 1)
WHERE a.username  = '${fine_username}'
)
SELECT
    A.xlmc
     ,count(1) AS sl
FROM
    (
        SELECT
            pq
             ,xm
             ,xlmc
        FROM ods_hr_view_zaizhi_user_d
        WHERE ny <= '${p_ym}'
          AND ny >=  '${p_startym}'
    )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
                ,'领悦集团总部'    AS hr_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 ('"+ p_area +"')", "")}
        AND CASE WHEN (SELECT mang_project FROM ods_one_to_many_perm WHERE username  = '${fine_username}' AND mang_organ IS NULL) IS NOT NULL
         THEN B.comm_name IN (SELECT mang_project FROM one_to_many WHERE username  = '${fine_username}')
          ELSE ${if(left(fine_role,2)=="项目","B.comm_name = '"+GETUSERDEPARTMENTS(3)+"'","1=1")} END

GROUP BY
    A.xlmc


-- ======================================
-- 职务角色
-- ======================================


SELECT
    zwje
     ,sum(sl) AS sl
FROM
    (
        SELECT
            zb	    -- 总部
             ,pq	    -- 片区
             ,xm	    -- 项目
             ,zwje	--	职务角色
             ,sl	    --	数量
        FROM dw.ods_hr_view_zwjs_d
    )A
        LEFT JOIN
    (
        SELECT
            organ_code
             ,hr_organ_id
             ,hr_comm_id
        FROM dim_organ_mapping
        GROUP BY
            organ_code
               ,hr_organ_id
               ,hr_comm_id
        UNION ALL
        SELECT
            '0000' AS organ_code    -- erp片区编码
             ,'153'    AS hr_organ_id  -- hr片区编码
             ,'153'    AS hr_comm_id    -- hr项目id
        FROM dual

    )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,"','")+"')", "")}
GROUP BY zwje

--    总部职能中心/一级部门负责人及以上人员
--   总部二级职能部门负责人
--   片区负责人
--   片区职能部门负责人
--   项目负责人
--   项目部门负责人
--   案场负责人
-- 案场部门负责人
-- 员工

with one_to_many AS(
SELECT
a.username AS username -- 用户名
,SUBSTRING_INDEX(SUBSTRING_INDEX(a.mang_project, ',',b.help_topic_id + 1),',',-1) AS mang_project -- 管理项目名
FROM ods_one_to_many_perm a
JOIN mysql.help_topic b
on b.help_topic_id < (LENGTH(a.mang_project) - length(replace(a.mang_project,',','')) + 1)
WHERE a.username  = '${fine_username}'
)
SELECT
    zwmc AS zwje
     ,count(1) AS sl
FROM
    (
        SELECT
            pq
             ,xm
             ,zwmc
        FROM ods_hr_view_zaizhi_user_d
        WHERE ny <= '${p_ym}'
        AND ny >=  '${p_startym}'
    )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
                ,'领悦集团总部'    AS hr_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 ('"+ p_area +"')", "")}
       AND CASE WHEN (SELECT mang_project FROM ods_one_to_many_perm WHERE username  = '${fine_username}' AND mang_organ IS NULL) IS NOT NULL
         THEN B.comm_name IN (SELECT mang_project FROM one_to_many WHERE username  = '${fine_username}')
          ELSE ${if(left(fine_role,2)=="项目","B.comm_name = '"+GETUSERDEPARTMENTS(3)+"'","1=1")} END

GROUP BY
    A.zwmc




-- ======================================
-- 职务层级
-- ======================================


SELECT
    swtx
     ,sum(sl) AS sl
FROM
    (
        SELECT
            zb	    -- 总部
             ,pq	    -- 片区
             ,xm	    -- 项目
             ,swtx	--	职务层级
             ,sl	    --	数量
        FROM dw.ods_hr_view_swtx_d
    )A
        LEFT JOIN
    (
        SELECT
            organ_code
             ,hr_organ_id
             ,hr_comm_id
        FROM dim_organ_mapping
        GROUP BY
            organ_code
               ,hr_organ_id
               ,hr_comm_id
        UNION ALL
        SELECT
            '0000' AS organ_code    -- erp片区编码
             ,'153'    AS hr_organ_id  -- hr片区编码
             ,'153'    AS hr_comm_id    -- hr项目id
        FROM dual
    )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,"','")+"')", "")}
GROUP BY swtx



with one_to_many AS(
SELECT
a.username AS username -- 用户名
,SUBSTRING_INDEX(SUBSTRING_INDEX(a.mang_project, ',',b.help_topic_id + 1),',',-1) AS mang_project -- 管理项目名
FROM ods_one_to_many_perm a
JOIN mysql.help_topic b
on b.help_topic_id < (LENGTH(a.mang_project) - length(replace(a.mang_project,',','')) + 1)
WHERE a.username  = '${fine_username}'
)
SELECT
    A.zycjname
     ,count(1) AS sl
FROM
    (
        SELECT
            pq
             ,xm
             ,zycjname
        FROM ods_hr_view_zaizhi_user_d
        WHERE ny <= '${p_ym}'
          AND ny >=  '${p_startym}'
    )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
                ,'领悦集团总部'    AS hr_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 ('"+ p_area +"')", "")}
        ${IF(LEN(p_area)>0," AND B.organ_code IN ('"+ p_area +"')", "")}
       AND CASE WHEN (SELECT mang_project FROM ods_one_to_many_perm WHERE username  = '${fine_username}' AND mang_organ IS NULL) IS NOT NULL
         THEN B.comm_name IN (SELECT mang_project FROM one_to_many WHERE username  = '${fine_username}')
          ELSE ${if(left(fine_role,2)=="项目","B.comm_name = '"+GETUSERDEPARTMENTS(3)+"'","1=1")} END

GROUP BY
    A.zycjname

-- ======================================
-- 一级专业条线
-- ======================================


SELECT
    zytx
     ,sum(sl) AS sl
FROM
    (
        SELECT
            zb	    -- 总部
             ,pq	    -- 片区
             ,xm	    -- 项目
             ,zytx	--	一级专业条线
             ,sl	    --	数量
        FROM dw.ods_hr_view_zytx_d
    )A
        LEFT JOIN
    (
        SELECT
            organ_code
             ,hr_organ_id
             ,hr_comm_id
        FROM dim_organ_mapping
        GROUP BY
            organ_code
               ,hr_organ_id
               ,hr_comm_id
        UNION ALL
        SELECT
            '0000' AS organ_code    -- erp片区编码
             ,'153'    AS hr_organ_id  -- hr片区编码
             ,'153'    AS hr_comm_id    -- hr项目id
        FROM dual

    )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,"','")+"')", "")}
GROUP BY zytx
;





with one_to_many AS(
SELECT
a.username AS username -- 用户名
,SUBSTRING_INDEX(SUBSTRING_INDEX(a.mang_project, ',',b.help_topic_id + 1),',',-1) AS mang_project -- 管理项目名
FROM ods_one_to_many_perm a
JOIN mysql.help_topic b
on b.help_topic_id < (LENGTH(a.mang_project) - length(replace(a.mang_project,',','')) + 1)
WHERE a.username  = '${fine_username}'
)
SELECT
    A.zytxmc
     ,count(1) AS sl
FROM
    (
        SELECT
            pq
             ,xm
             ,zytxmc
        FROM ods_hr_view_zaizhi_user_d
        WHERE ny <= '${p_ym}'
          AND ny >=  '${p_startym}'
    )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
                ,'领悦集团总部'    AS hr_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 ('"+ p_area +"')", "")}
       AND CASE WHEN (SELECT mang_project FROM ods_one_to_many_perm WHERE username  = '${fine_username}' AND mang_organ IS NULL) IS NOT NULL
         THEN B.comm_name IN (SELECT mang_project FROM one_to_many WHERE username  = '${fine_username}')
          ELSE ${if(left(fine_role,2)=="项目","B.comm_name = '"+GETUSERDEPARTMENTS(3)+"'","1=1")} END

GROUP BY
    A.zytxmc


-- ======================================
-- 人员明细
-- ======================================

SELECT
      A.pq	AS pq -- 片区
     ,B.hr_organ_name AS hr_organ_name
     ,A.xm	AS xm -- 项目
     ,B.hr_comm_name AS hr_comm_name
     ,A.bm	AS bm   -- 部门id
     ,A.bmmc AS bmmc	-- 部门名称
     ,A.gwid AS gwid -- 岗位id
     ,A.gwmc AS	 gwmc -- 岗位名称
     ,A.id	AS  id  -- 人员id
     ,A.workcode AS workcode	-- 工号
     ,A.lastname AS lastname	-- 姓名
     ,A.nl	AS nl   -- 年龄
     ,A.sl  AS sl   -- 司龄
     ,A.zwid AS zwid    -- 职务角色id
     ,A.zwmc AS zwmc    -- 职务角色名称
     ,A.zytxid	AS zytxid -- 专业条线id
     ,A.zytxmc	AS zytxmc -- 专业条线名称
     ,A.zycjid	AS zycjid -- 职员层级id
     ,A.zycjname AS zycjname	--	职员层级名称
FROM
(
    SELECT
     pq	-- 片区
    ,xm	-- 项目
    ,bm	-- 部门id
    ,bmmc	-- 部门名称
    ,gwid -- 岗位id
    ,gwmc	 -- 岗位名称
    ,id	-- 人员id
    ,workcode	-- 工号
    ,lastname	-- 姓名
    ,nl	 -- 年龄
    ,sl -- 司龄
    ,zwid --	职务角色id
    ,zwmc -- 职务角色名称
    ,zytxid	-- 	专业条线id
    ,zytxmc	--	专业条线名称
    ,zycjid	-- 职员层级id
    ,zycjname	--	职员层级名称
    FROM ods_hr_view_zaizhi_user_d
    WHERE LEFT(ny,7) = '${p_ym}'
)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
    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
where 1 = 1
${IF(LEN(p_area)>0," AND B.organ_code IN ('"+ p_area +"')", "")}


-- ======================================
-- 使用ods_hr_view_zaizhi_user_d表计算人员分布占比
-- ======================================
