当前位置: 首页 > 面试题库 >

查找两个日期之间覆盖范围更广的员工的更有效方法

单于翰飞
2023-03-14
问题内容

我需要检索一份雇员列表,并为每位雇员列出他们在给定年度内积极参与福利保障的几个月的清单。有一个包含工作数据的表和一个包含福利信息的表。还有一个交付日期表,该表列出了2007-2018年的每个日期,并针对每个日期显示月,日和日历年。

我现在编写查询的方式是说:在日期表中找到提示年份的01/01到12/31之间的所有日期(或当前日期,以较早的日期为准),2)员工在福利表上活跃的时间。对于每个日期,我还希望截至该日期的职位表中的部门名称和福利表中的福利计划。然后我做一个不同的,只显示每个员工的月份和日历年。

这行得通,但是当我尝试为其中有很多人的部门这样做时,问题就来了。我认为运行起来需要很长时间,因为它只为每个雇员检索最多365行,然后仅显示其中的12行,因为这仅拉开了不同的月份。我觉得有更好的方法可以做到这一点,我只是想不出它是什么。

这是我正在使用的表的一些简化示例:

THE_DATE   MONTHOFYEAR   CALENDAR_YEAR
01-OCT-15  10            2015
02-OCT-15  10            2015
03-OCT-15  10            2015
...

Jobs Table

(A=Active; I=Inactive)

EMPLID     EFFDT         DEPTID           HR_STATUS
00123      01-FEB-15     900              A
00123      30-JUN-15     900              I
00123      01-AUG-15     901              A

Benefits Table

EMPLID     EFFDT         BENEFIT_PLAN     STATUS
00123      01-MAR-15     PPO              A
00123      31-JUL-15                      I
00123      01-SEP-15     HMO              A

所需结果

EMPLID     CALENDAR_YEAR MONTHOFYEAR      DEPTID         BENEFIT_PLAN
00123      2015          3                900            PPO
00123      2015          4                900            PPO
00123      2015          5                900            PPO
00123      2015          6                900            PPO
00123      2015          7                900            PPO
00123      2015          9                901            HMO
00123      2015          10               901            HMO
00123      2015          11               901            HMO
^ (shows November row even though employee was only covered for part of this month)

示例SQL以获取上面的结果

SELECT DISTINCT J.EMPLID, D.CALENDAR_YEAR, D.MONTHOFYEAR, J.DEPTID, B.BENEFIT_PLAN
FROM DATES D, 
     JOBS J 
     JOIN 
     BENEFITS B 
     ON J.EMPLID = B.EMPLID
WHERE D.THE_DATE <= SYSDATE
AND D.THE_DATE BETWEEN 
        TO_DATE(:YEAR_PROMPT || '01-01', 'YYYY-MM-DD') 
        AND 
        TO_DATE(:YEAR_PROMPT || '12-31', 'YYYY-MM-DD')
AND B.STATUS = 'A'
AND D.THE_DATE BETWEEN 
        B.EFFDT 
        AND 
        NVL(SELECT MIN(B_ED.EFFDT) 
            FROM BENEFITS B_ED
            WHERE B_ED.EMPLID = B.EMPLID
            AND B_ED.EFFDT > B.EFFDT
        , SYSDATE)
AND J.EFFDT = (SELECT MAX(J_ED.EFFDT)
               FROM JOBS J_ED
               WHERE J_ED.EMPLID = J.EMPLID
               AND J_ED.EFFDT <= D.THE_DATE)

不用说“检索每个日期并检查是否符合条件”,我可以以某种方式改变逻辑以得到相同的结果而无需经过太多行吗?


问题答案:

是的; 通过使用LEAD()分析功能,您可以计算作业和收益表中的下一个effdt,这使得在范围之间进行查询变得更加容易。

就像是:

with dates as (select trunc(sysdate, 'yyyy') - 1 + level the_date,
                      to_number(to_char(trunc(sysdate, 'yyyy') - 1 + level, 'mm')) monthofyear,
                      to_number(to_char(sysdate, 'yyyy')) calendar_year
               from   dual
               connect by level <= 365),
      jobs as (select 123 emplid, to_date('01/02/2015', 'dd/mm/yyyy') effdt, 900 deptid, 'A' hr_status from dual union all
               select 123 emplid, to_date('30/06/2015', 'dd/mm/yyyy') effdt, 900 deptid, 'I' hr_status from dual union all
               select 123 emplid, to_date('01/08/2015', 'dd/mm/yyyy') effdt, 901 deptid, 'A' hr_status from dual),
  benefits as (select 123 emplid, to_date('01/03/2015', 'dd/mm/yyyy') effdt, 'PPO' benefit_plan, 'A' status from dual union all
               select 123 emplid, to_date('31/07/2015', 'dd/mm/yyyy') effdt, null benefit_plan, 'I' status from dual union all
               select 123 emplid, to_date('01/09/2015', 'dd/mm/yyyy') effdt, 'HMO' benefit_plan, 'A' status from dual),
-- ********* end of mimicking your tables ********* --
         j as (select emplid,
                      effdt,
                      deptid,
                      hr_status,
                      lead(effdt, 1, sysdate) over (partition by emplid order by effdt) next_effdt
               from   jobs),
         b as (select emplid,
                      effdt,
                      benefit_plan,
                      status,
                      lead(effdt, 1, sysdate) over (partition by emplid order by effdt) next_effdt
               from   benefits)
select distinct j.emplid,
                d.calendar_year,
                d.monthofyear,
                j.deptid,
                b.benefit_plan
from   j
       inner join dates d on (d.the_date >= j.effdt and d.the_date < j.next_effdt)
       inner join b on (j.emplid = b.emplid)
where  d.the_date <= sysdate
and    d.the_date between to_date (:year_prompt || '01-01', 'YYYY-MM-DD')
                      and to_date (:year_prompt || '12-31', 'YYYY-MM-DD') -- if no index on d.the_date, maybe use trunc(the_date, 'yyyy') = :year_prompt
and    b.status = 'A'
and    d.the_date between b.effdt and b.next_effdt
order by 1, 4, 2, 3;

    EMPLID CALENDAR_YEAR MONTHOFYEAR     DEPTID BENEFIT_PLAN
---------- ------------- ----------- ---------- ------------
       123          2015           3        900 PPO         
       123          2015           4        900 PPO         
       123          2015           5        900 PPO         
       123          2015           6        900 PPO         
       123          2015           7        900 PPO         
       123          2015           9        901 HMO         
       123          2015          10        901 HMO         
       123          2015          11        901 HMO

(很明显,你可以排除datesjobsbenefits从上面的查询子查询,既然你已经有了这些表。他们只是在查询中,以模拟其在它的数据表,而无需实际创建表)。

预计到达时间:这是一个仅根据传入的年份计算12个月的版本,它将日期行减少到12,而不是365/366行。

不幸的是,在同一个月开始有多行时,您仍需要使用不同的字符。

例如,对于以下示例中的数据,如果删除了不重复的数据,则第6个月将有3行。但是,区别操作所操作的行数将大大少于以前的行数。

with dates as (select add_months(to_date(:year_prompt || '-01-01', 'YYYY-MM-DD'), - 1 + level) the_date,
                      level monthofyear,
                      :year_prompt calendar_year -- assuming this is a number
               from   dual
               connect by level <= 12),
      jobs as (select 123 emplid, to_date('01/02/2015', 'dd/mm/yyyy') effdt, 900 deptid, 'A' hr_status from dual union all
               select 123 emplid, to_date('15/06/2015', 'dd/mm/yyyy') effdt, 900 deptid, 'I' hr_status from dual union all
               select 123 emplid, to_date('26/06/2015', 'dd/mm/yyyy') effdt, 900 deptid, 'A' hr_status from dual union all
               select 123 emplid, to_date('01/08/2015', 'dd/mm/yyyy') effdt, 901 deptid, 'A' hr_status from dual),
  benefits as (select 123 emplid, to_date('01/03/2015', 'dd/mm/yyyy') effdt, 'PPO' benefit_plan, 'A' status from dual union all
               select 123 emplid, to_date('31/07/2015', 'dd/mm/yyyy') effdt, null benefit_plan, 'I' status from dual union all
               select 123 emplid, to_date('01/09/2015', 'dd/mm/yyyy') effdt, 'HMO' benefit_plan, 'A' status from dual),
-- ********* end of mimicking your tables ********* --
         j as (select emplid,
                      trunc(effdt, 'mm') effdt,
                      deptid,
                      hr_status,
                      trunc(coalesce(lead(effdt) over (partition by emplid order by effdt) -1, sysdate), 'mm') end_effdt
                        -- subtracting 1 from the lead(effdt) since here since the original sql had d.the_date < j.next_effdt and we need
                        -- to take into account when the next_effdt is the first of the month; we want the previous month to be displayed
               from   jobs),
         b as (select emplid,
                      trunc(effdt, 'mm') effdt,
                      benefit_plan,
                      status,
                      trunc(lead(effdt, 1, sysdate) over (partition by emplid order by effdt), 'mm') end_effdt
               from   benefits)
select distinct j.emplid,
                d.calendar_year,
                d.monthofyear,
                j.deptid,
                b.benefit_plan
from   j
       inner join dates d on (d.the_date between j.effdt and j.end_effdt)
       inner join b on (j.emplid = b.emplid)
where  d.the_date <= sysdate
and    b.status = 'A'
and    d.the_date between b.effdt and b.end_effdt
order by 1, 4, 2, 3;

    EMPLID CALENDAR_YEAR MONTHOFYEAR     DEPTID BENEFIT_PLAN                    
---------- ------------- ----------- ---------- --------------------------------
       123 2015                    3        900 PPO                             
       123 2015                    4        900 PPO                             
       123 2015                    5        900 PPO                             
       123 2015                    6        900 PPO                             
       123 2015                    6        900 PPO                             
       123 2015                    7        900 PPO                             
       123 2015                    9        901 HMO                             
       123 2015                   10        901 HMO                             
       123 2015                   11        901 HMO


 类似资料:
  • 很久以前,我在准备面试时遇到了这个问题,认为这是一个需要解决的有趣问题。问题是这样的:找到一组区间的覆盖范围例如-给定[1,4)、[-2,3)、[9,10):输出应该是7(区间集覆盖-2,-1,0,1,2,3,9)。 我最初的方法是迭代区间集;将每个间隔中的数字添加到已排序的链表中。如果排序列表中已存在任何数字,请跳过它。我相信这需要O(N^2)时间和O(N)空间,所以我们可能会做得更好。 或者,

  • 问题内容: 我的数据库中有以下一组匹配日期的日期(dd / MM / yyyy): 事件具有开始和结束日期(时间无关紧要),并且endDate为NULL表示事件仍在进行中。 我想确定的是两个任意日期之间的日期范围,其中a)没有事件,b)事件重叠。 因此,对于输入日期范围01/04/2009-30/06/2009,我希望得到以下结果: 注意,作为结果,两个相邻的重叠范围是可以接受的。 谁能用SQL算

  • 问题内容: 我的问题类似于以下问题: http://asktom.oracle.com/pls/asktom/f?p=100:11:0:::::P11_QUESTION_ID:14582643282111 区别在于我的内部查询返回两条记录,而我有外部查询。 我需要编写类似这样的内部查询,这将为我提供两个日期范围之间的日期列表(我正在尝试不执行此查询)。 我的内部查询返回以下2行: 所以我需要内部查

  • 问题内容: 如何使用PHP查找两个日期之间的天数? 问题答案:

  • 我们希望您能够帮助我们解决以下问题: 给出了一个可能包含圈的有向图。必须找到一组满足以下标准的路径: 在从节点A到节点B的过程中可以通过的所有边必须被集合内的路径覆盖(一条边可以是集合中多条路径的一部分) 解决方案不必是路径数最少的解决方案,路径也不必是最短的。然而,该解决方案应该可以像java一样使用编程语言高效地实现。我们需要解决方案来生成几个测试用例,覆盖节点a和节点B之间的所有边很重要。

  • 问题内容: Excel具有NETWORKDAYS()函数,该函数可查找两个日期之间的工作日数。 有人对MySQL有类似功能吗?由于假期增加了复杂性,因此该解决方案不必处理假期。 问题答案: 这个表达- 计算开始日期@S和结束日期@E之间的工作日数。 假设结束日期(@E)不早于开始日期(@S)。与DATEDIFF兼容,因为相同的开始日期和结束日期为零个工作日。忽略假期。 数字字符串的结构如下。创建一