如何确定两个请假(休假)是否冲突(重叠)

时间:2015-08-18 13:42:31

标签: tsql sql-server-2008-r2

我有一个人力资源系统,我需要查看新请假(休假)请求是否与现有请求冲突。我已经基于整天做了这个,但我需要根据半天进行比较...

这是我的数据结构(和数据格式)这都是在SQL Server 2008 R2中完成的。

StartDate     (datetime)
EndDate       (datetime)
'The following 2 variables show if the user is taking a half or a full day off. 
'The options for each are listed and explained.

StartDateAMPM (int) 0=All Day (StartDate may or may not be the same as the EndDate), 
                    1=AM, (User is out of the office only in the morning)
                    2=PM  (User is out of the office only in the afternoon)

EndDateAMPM   (int) 0="All Day" / "half day" / "StartDate = EndDate", 
                    1=AM (The user is off for the morning only)
                     **NOTE This cannot be PM (as you cannot end a leave
                            with a half day in the afternoon)
                     **IF this is a "0", there are 3 possibilities --
                      A) a full day leave where the StartDate <> EndDate
                      B) a full day where the StartDate = EndDate
                      C) a partial day where StartDate = EndDate and the user
                              is off in the morning or the afternoon

所以我将此信息传递给数据库,并且我正在尝试查看新的假设是否与现有的假期发生冲突......以下是一天内执行此操作的代码:

Legend:  @Start DateTime -- StartDate from new leave
         @end   DateTime -- EndDate from new leave
         @StartDateAMPM (int) 0,1,2 (see above for details)
         EndDateAMPM    (int) 0,1   (see above for details)

select count(ID)  from v_VacReqWHalfDays 
where 
  ( @Start BETWEEN StartDate and EndDate 
OR  @End   BETWEEN StartDate and EndDate 
OR (@Start <= StartDate and @End >=EndDate))
and kUserID = @UserID

如果Count(ID)&gt; 0,存在冲突 -

我的问题是如何纳入休假半天...... (人们可以休息整天或半天(早上或下午)。

我想使用TSQL来确定当前请假请求是否与现有请求冲突(重叠),并且无法弄清楚如何执行此操作...

1 个答案:

答案 0 :(得分:0)

我意识到这已被问及并回答了......  Algorithm to detect overlapping periods ...... 谢谢大家...这是一个重复的问题。请忽略......我意识到我应该把假期的实际时间戳添加到我的开始日期和结束日期并做一个简单的比较 - 我希望我几个月前已经想到这个......本来可以节省我很多痛苦和痛苦......

My new startDate will be recorded as "08/22/2015 09:00:00.000" 
                          instead of "08/22/2015 00:00:00.000"  
and the endDate will be              "08/23/2015 13:00:00.000" 
                          instead of "08/23/2015 00:00:00.000"  
I can then compare as per the linked ticket: