按日期计算的活跃员工总数

时间:2013-05-21 18:09:33

标签: sql oracle oracle11g

我在过去的书面查询中给出了按日期计算的数据(雇用,终止等等),如下所示:

SELECT per.date_start AS "Date",
  COUNT(peo.EMPLOYEE_NUMBER) AS "Hires"

FROM hr.per_all_people_f peo,
  hr.per_periods_of_service per

WHERE per.date_start BETWEEN peo.effective_start_date AND peo.EFFECTIVE_END_DATE

AND per.date_start BETWEEN :PerStart AND :PerEnd

AND per.person_id = peo.person_id

GROUP BY per.date_start

我现在想要按日期创建一个活跃员工的数量,但是我不确定如何约会查询,因为我使用一个范围来确定活动:

SELECT COUNT(peo.EMPLOYEE_NUMBER) AS "CT"

FROM hr.per_all_people_f peo

WHERE peo.current_employee_flag = 'Y'

and TRUNC(sysdate) BETWEEN peo.effective_start_date AND peo.EFFECTIVE_END_DATE

4 个答案:

答案 0 :(得分:2)

这是一种简单的入门方式。这适用于数据中的所有有效日期和结束日期:

select thedate,
       SUM(num) over (order by thedate) as numActives
from ((select effective_start_date as thedate, 1 as num from hr.per_periods_of_service) union all
      (select effective_end_date as thedate, -1 as num from hr.per_periods_of_service)
     ) dates

它的工作原理是为每个开始添加一个人,并为每个末尾减去一个(通过num)并进行累计求和。这可能有重复的日期,因此您也可以进行聚合以消除这些重复项:

select thedate, max(numActives)
from (select thedate,
             SUM(num) over (order by thedate) as numActives
      from ((select effective_start_date as thedate, 1 as num from hr.per_periods_of_service) union all
            (select effective_end_date as thedate, -1 as num from hr.per_periods_of_service)
           ) dates
     ) t
group by thedate;

如果您真的想要所有日期,那么最好从日历表开始,并在原始查询中使用简单的变体:

select c.thedate, count(*) as NumActives
from calendar c left outer join
     hr.per_periods_of_service pos
     on c.thedate between pos.effective_start_date and pos.effective_end_date
group by c.thedate;

答案 1 :(得分:0)

如果您想计算在整个输入日期范围内活跃的所有员工

SELECT COUNT(peo.EMPLOYEE_NUMBER) AS "CT"
FROM hr.per_all_people_f peo
WHERE peo.[EFFECTIVE_START_DATE] <= :StartDate   
AND  (peo.[EFFECTIVE_END_DATE] IS NULL OR peo.[EFFECTIVE_END_DATE] >= :EndDate)  

答案 2 :(得分:0)

以下是基于Gordon Linoff回答的示例 稍加修改,因为在SUBSTRACT表中,所有记录在NUM中都显示为-1,即使END DATE = NULL中没有日期。

use AdventureWorksDW2012  --using in MS SSMS for choosing DATABASE to work with
                          -- and may be not work in other platforms

select
    t.thedate
    ,max(t.numActives) AS "Total Active Employees"
from (
        select
            dates.thedate
            ,SUM(dates.num) over (order by dates.thedate) as numActives
        from
            (
                (
                select
                    StartDate as thedate
                    ,1 as num
                from DimEmployee
                )
                    union all
                        (
                        select
                            EndDate as thedate
                            ,-1 as num
                        from DimEmployee
                        where EndDate IS NOT NULL
                        )
            ) AS dates
     ) AS t
group by thedate
ORDER BY thedate

为我工作,希望它会帮助某人

答案 3 :(得分:0)

我能够通过以下方式获得我想要的结果:

--Active Team Members by Date
SELECT "a_date",
  COUNT(peo.EMPLOYEE_NUMBER) AS "CT"
FROM hr.per_all_people_f peo,
  (SELECT DATE '2012-04-01'-1 + LEVEL AS "a_date"
  FROM dual
    CONNECT BY LEVEL <= DATE '2012-04-30'+2 - DATE '2012-04-01'-1
  )
WHERE peo.current_employee_flag = 'Y'
AND "a_date" BETWEEN peo.effective_start_date AND peo.EFFECTIVE_END_DATE
GROUP BY "a_date"
ORDER BY "a_date"