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

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



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

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





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


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



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


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






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


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





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


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
;

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

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