sql查询以按月获取特定部门的员工人数

dzjeubhm  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(444)

在系统中定义了组织层级,即划分最高,然后是地区,然后是部门。部门已标记为员工。我创建了一个提取正确数据的查询。类似于-

DIVISION         DISTRICT        DEPARTMENT              PERSON_NUMBER        effective_start_date          effective_end_date
CA Division         Grande          XYZ                     5875                 01-05-2020                    31-12-4712
CA Division         Grande          XYZ                     6729                 02-02-2020                    31-12-4712
CA Division         Grande          XYZ                     6005                 30-05-2020                    31-12-4712
CA Division         Deer            Deer Tubing             6836                 01-01-2020                    31-12-4712
CA Division         Grande          XYZ  Fracturing         1934                 15-02-2020                    31-12-4712
CA Division         Grande          XYZ  Fracturing         6931                 02-02-2020                    31-12-4712

我使用的查询如下-

The query i used is as follows - 
WITH ORG_TREE
AS (
    SELECT /*+ materialize */
        DISTINCT *
    FROM (
        SELECT (
                SELECT p.name
                FROM hr_all_organization_units_f_vl p
                WHERE p.organization_id = ot.parent_organization_id
                    AND rownum = 1
                ) parent_org_name
            ,(
                SELECT c.name
                FROM hr_all_organization_units_f_vl c
                WHERE c.organization_id = ot.organization_id
                    AND rownum = 1
                ) org_name
            ,(
                SELECT c.attribute_number1
                FROM hr_all_organization_units_f_vl c
                WHERE c.organization_id = ot.organization_id
                    AND rownum = 1
                ) org_legacy
            ,ot.tree_structure_code
            ,ot.parent_organization_id parent_org_id
            ,ot.organization_id org_id
            ,LEVEL levelcount
        FROM PER_DEPT_TREE_NODE_V ot
        WHERE 1=1
        and ot.tree_structure_code = 'PER_DEPT_TREE_STRUCTURE'
            AND ot.tree_code = 'CWS_DEPT_HIERARCHY' 
            START
        WITH ot.parent_organization_id IS NULL CONNECT BY PRIOR ot.organization_id = ot.parent_organization_id
        )
    ORDER BY levelcount ASC
    )
    ,flattened_tree
AS (
    SELECT /*+ materialize */
        lev01.ORG_NAME LVL1_ORG
        --,lev02.ORG_NAME LVL2_ORG
        ,lev03.ORG_NAME division
        ,lev04.ORG_NAME District
        ,lev05.ORG_NAME department
        ,lev05.org_id department_id
    FROM ORG_TREE lev01
    LEFT OUTER JOIN ORG_TREE lev02 ON lev01.org_id = lev02.parent_org_id
    LEFT OUTER JOIN ORG_TREE lev03 ON lev02.org_id = lev03.parent_org_id
    LEFT OUTER JOIN ORG_TREE lev04 ON lev03.org_id = lev04.parent_org_id
    LEFT OUTER JOIN ORG_TREE lev05 ON lev04.org_id = lev05.parent_org_id
    LEFT OUTER JOIN ORG_TREE lev06 ON lev05.org_id = lev06.parent_org_id
    LEFT OUTER JOIN ORG_TREE lev07 ON lev06.org_id = lev07.parent_org_id
    WHERE lev01.PARENT_ORG_NAME IS NULL
    and lev02.ORG_NAME = 'Canada'
    )
select division,
District,
department,
papf.person_number,
paam.effective_start_date,
paam.effective_end_date
 from
 flattened_tree,
 HR_ORG_UNIT_CLASSIFICATIONS_F houcf, 

HR_ALL_ORGANIZATION_UNITS_F haouf, 

HR_ORGANIZATION_UNITS_F_TL hauft,
per_all_assignments_m paam,
per_all_people_f papf
 where haouf.organization_id = flattened_tree.department_id
and haouf.ORGANIZATION_ID = houcf.ORGANIZATION_ID 

AND haouf.ORGANIZATION_ID = hauft.ORGANIZATION_ID 

AND haouf.EFFECTIVE_START_DATE BETWEEN houcf.EFFECTIVE_START_DATE AND houcf.EFFECTIVE_END_DATE 

AND hauft.LANGUAGE = 'US'

AND hauft.EFFECTIVE_START_DATE = haouf.EFFECTIVE_START_DATE 

AND     hauft.EFFECTIVE_END_DATE = haouf.EFFECTIVE_END_DATE 

AND     houcf.CLASSIFICATION_CODE = 'DEPARTMENT' 

AND     trunc(SYSDATE) BETWEEN hauft.effective_start_date AND hauft.effective_end_date

AND     hauft.organization_id = paam.organization_id

and     paam.person_id = papf.person_id

and     paam.primary_assignment_flag = 'Y'

and     paam.assignment_type = 'E'
and     paam.ASSIGNMENT_STATUS_TYPE in ( 'ACTIVE','SUSPENDED')

and     paam.effective_latest_change = 'Y'

and     trunc(SYSDATE) between paam.effective_start_date and paam.effective_end_date

and     trunc(SYSDATE) between papf.effective_start_date and papf.effective_end_date

and division = 'CA Division'

现在,我希望输出显示特定部门在特定月份的人数。类似于-
如果我通过month='一月'

DIVISION            DISTRICT        DEPARTMENT              JAN
CA Division         Grande          XYZ                     0
                    Deer            Deer Tubing             1
                                    XYZ  Fracturing         0

如果我过了月份-“五月”所有的记录都会来,因为记录从一月/二月一直存在到五月

DIVISION            DISTRICT        DEPARTMENT              JAN
CA Division         Grande          XYZ                     3
                    Deer            Deer Tubing             1
                                    XYZ  Fracturing         2

如何调整查询以获得像这样的输出和这种格式?

hts6caw3

hts6caw31#

这是我的建议

-- Assming the query is for 1 year
    /*
    Find the date range based on your parameter
    ex. if you pass JANUARY, StartDate = 01-01-2020 to 01-31-2020 
    ex. if you pass MAY, StartDate = 01-01-2020 to EndDate = 05-31-2020 
    ... and so on
    */

    Select DIVISION, DISTRICT, DEPARTMENT, Count(PERSON_NUMBER)
    From
    (
        -- Your Query
    ) Q
    Where effective_start_date >= StartDate and effective_start_date <= EndDate 
    Group By DIVISION, DISTRICT, DEPARTMENT;

相关问题