选择每个部门的重叠假期

时间:2015-11-11 15:42:40

标签: sql sql-server date

我正在尝试检查我公司的任何员工是否要求重叠假期。这项政策是每个部门只允许一个人一次性关闭。

我应该使用什么查询? (我是菜鸟,所以告诉我你是否需要更多信息)。

我想要使用的表的示例:

Request ID            Status
1                  Not Approved, overlapping 26-27/12
2                  Not Approved, overlapping 09-14/06
3                  Not Approved, overlapping completely
4                  Not Approved, overlapping 09-14/06
5                  Approved, not overlapping in this department
6                  Approved, not overlapping in this department

结果:

{{1}}

在第二阶段,我想比较请求的假期是否在一周之内,包含银行假日(我将在银行假期有不同的表格)。

提前致谢!

2 个答案:

答案 0 :(得分:1)

一种方法是exists

select e.*,
       (case when exists (select 1
                          from example e2
                          where e2.departmentid = e.departmentid and
                                e2.employeeid <> e.employeeid and
                                e2.startdate <= e.enddate and
                                e2.enddate >= e.startdate
                         )
             then 'Overlapping'
             else 'NotOverlapping'
        end) as Status
from example e;

获取完整的信息比较复杂,取决于数据库。问题是多重叠。

实际上,我们可以在没有太多问题的情况下获得更多信息:

select e.RequestId,
       (case when count(e2.RequestId) = 0
             then 'Approved, not overlapping in this department'
             when count(e2.RequestId) = 1
             then (case when min(e2.startdate) <= e.startdate and
                             max(e2.enddate) >= e.enddate
                        then 'Not Approved, overlapping completely'
                        else 'Not Approved, overlapping partially'
                   end)
             else 'Not Approved, multiple overlaps'
        end) as Status
from example e left join
     example e2
     on e2.departmentid = e.departmentid and
        e2.startdate <= e.enddate and
        e2.enddate >= e.startdate and
        e2.employeeid <> e.employeeid
group by e.RequestId, e.startdate, e.enddate;

在不知道数据库的情况下获取实际日期比较棘手。

答案 1 :(得分:0)

browser.min.js
顺便说一句,第一个雇主假期太大 - 超过1年:) 另一个StartDate&gt; EndDate(它是什么?)

结果 enter image description here