Dan*_*n B 6 sql-server t-sql sql-server-2008-r2
我有一个 HR 系统,我需要检测新的请假(休假)请求是否与现有的请求冲突。我已经根据一整天的时间编写了代码来做到这一点;我现在需要做基于半天的比较。
这是我的数据结构(和数据格式)。我正在使用 SQL Server 2008 R2。
StartDate (datetime)
EndDate (datetime)
Run Code Online (Sandbox Code Playgroud)
以下 2 个变量显示用户是休息半天还是一整天。列出并解释了每个选项的选项。
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
Run Code Online (Sandbox Code Playgroud)
我将此信息传递给数据库,并尝试查看新假期是否与现有假期冲突。这是一天的代码:
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)
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
如果Count(ID) > 0
,则存在冲突。
我的问题是如何合并 1/2 天假期。人们可以休息一整天或半天(上午或下午)。
我想使用 TSQL 来确定当前请假请求是否与现有请假请求冲突(重叠)并且无法弄清楚如何去做。
我意识到这个问题之前已经在 Stack Overflow 上得到了回答。它让我朝着正确的方向前进。
我现在意识到我应该将休假的实际时间戳添加到我的开始日期和结束日期中并进行简单的比较。我希望几个月前我就明白了这一点;这会减轻我很多痛苦。
对于将来遇到类似问题的任何人:
StartDate
将被记录为08/22/2015 09:00.000
而不是08/22/2015 00:00.000
EndDate
是8/23/2015 13:00:00.000
然后我可以根据链接的答案进行比较:
(
(@Start > StartDate and @Start < EndDate)
OR (@End > StartDate and @End < EndDate)
OR (@Start < StartDate and @End >EndDate)
)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12316 次 |
最近记录: |