2015-08-18 120 views
0

我有一個人力資源系統,我需要看看新的休假(休假)請求是否與現有請求衝突。我這樣做是基於整天,但我需要做半天的比較......我如何確定兩個請假(休假)衝突(重疊)

這是我的數據結構(和數據格式)這一切都在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 

如果計數(ID)> 0,存在衝突 -

我的問題是如何納入1/2天假...(人們可以採取全天或半天休息(早上或下午)

我想用TSQL來確定當前的休假請求衝突(重疊)與現有的和無法弄清楚如何做到這一點?

+0

重疊的一般檢查是:'Start1 <= End2和Start2 <= End1'。 – HABO

+0

如果日期範圍相同,這不起作用!檢查出來......讓我知道如果我錯了......發生過一次;-) –

+0

'declare @ Start1 as Date ='19960218',@ End1 as Date ='19960315'; declare @ Start2 as Date = @ Start1,@ End2 as Date = @ End1; select @ Start1作爲Start1,@ End1作爲End1,@ Start2作爲Start2,@ End2作爲End2,@ Start1 <= @ End2和@ Start2 <= @ End1時的情況,然後'重疊'else'無重疊'結束作爲狀態; '也許你遇到了沒有匹配時間的'DATETIME'值問題。 – HABO

回答

0

我知道這已被要求,並回答了... Algorithm to detect overlapping periods ... 謝謝大家......這是一個dup問題。請忽略......我知道我應該把實際的假期時間戳添加到我的開始日期和結束日期,並做一個簡單的比較 - 我希望在幾個月前我已經明白了這一點......會爲我節省很多疼痛和痛苦......

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: