如何获得在职员工数(所有月份)

时间:2019-07-14 14:14:51

标签: sql oracle fusion

我正在尝试在(Fusion BI报表)中创建HCM报表,以检索所选年份“所有月份”的员工人数。

在当前情况下,当我们在“ 2017”上运行报表时,它会检索9月,8月,9月的员工总数

需要什么: 如果参数=“ 2017”且9月之后的月份为空,则获取“ 9月”中的最后一个现有值

Month Name   Current result    Expected result
July          1                 1
Aug           3                 3 
Sept          4                 4
Oct                             4
Nov                             4
Dec                             4

我的查询

SELECT PIVOT_TBL.* FROM
(SELECT
        COUNT(PAPF.PERSON_ID) OVER (ORDER BY  TO_CHAR(PAPF.START_DATE,'MM-YYYY')) AS OP_BALANCE,
        EXTRACT (MONTH FROM PAPF.START_DATE)  AS OMONTHS


 FROM
        PER_ALL_PEOPLE_F          PAPF,
        PER_ALL_ASSIGNMENTS_M     PAAM,   
        PER_PERSONS               PP  

WHERE     
        PAAM.PERSON_ID              =  PAPF.PERSON_ID
    AND PP.PERSON_ID                =  PAPF.PERSON_ID  
    ------------------------------------------------ 
    AND PAAM.ASSIGNMENT_TYPE        =  'E'
    AND PAAM.PRIMARY_FLAG           =  'Y'  
    AND PAAM.EFFECTIVE_LATEST_CHANGE = 'Y' 
    AND PAAM.ASSIGNMENT_STATUS_TYPE  = 'ACTIVE'
    -------------------------------------------------
    AND TRUNC(SYSDATE) BETWEEN PAPF.EFFECTIVE_START_DATE    AND  PAPF.EFFECTIVE_END_DATE 
    AND TRUNC(SYSDATE) BETWEEN PAAM.EFFECTIVE_START_DATE    AND  PAAM.EFFECTIVE_END_DATE  
    AND TO_CHAR(PAPF.START_DATE,'YYYY')='2017'
    ORDER BY 2)SRC_TABLE

    PIVOT
    (MAX(OP_BALANCE) AS EJ FOR OMONTHS IN
    ('1' AS JAN , '2' AS FEB , '3' AS MAR , '4'  AS APR , '5'  AS MAY , '6'  AS JUNE ,
     '7' AS JUL , '8' AS AUG , '9' AS SEPT, '10' AS OCTO, '11' AS NOV , '12' AS DECE)
    )PIVOT_TBL

1 个答案:

答案 0 :(得分:1)

您必须确保所有月份都以某种方式存在,然后将LAST_VALUE()函数与IGNORE NULLS配合使用,以复制下个月的最后一个非空值。

var yr number;
exec :yr := 2017;
with raw_results(dte, rslt) as (
  select date '2017-07-01', 1 from dual union all
  select date '2017-08-01', 3 from dual union all
  select date '2017-09-01', 4 from dual
)
, min_dte as (
  select min(dte) dte from raw_results
  where dte >= trunc(to_date(:yr,'yyyy'), 'yy')
)
, months as (
  select add_months(dte, level-1) dte
  from min_dte
  connect by extract(year from add_months(dte, level-1)) = :yr
)
select to_char(dte, 'Mon') "Month", 
  last_value(rslt ignore nulls) over(order by dte) "Result"
from months
left join raw_results using(dte);

Month            Result
------------ ----------
Jul                   1
Aug                   3
Sep                   4
Oct                   4
Nov                   4
Dec                   4