2011-02-22 41 views
0

使用SQL Server進行身份2005如何通過比較日期

休假表

ID StartDate EndDate 

001 04/01/2010 04/02/2010 
002 04/02/2010 04/03/2010 
… 

事件表

ID Date PresentDate Status 

001 03/30/2010 03/30/2010 Present 
001 03/31/2010 null  absent 
001 04/01/2010 null  Leave 
001 04/02/2010 null  Leave 
001 04/03/2010 null  absent 
001 04/04/2010 04/04/2010 Present 
…. 

所有Datecolumn數據類型爲datetime

在狀態列,如果當前日期爲空,則它將顯示爲「不存在」,如果不爲空,則它將顯示y作爲「禮物」。現在,如果我們爲日期申請假,那麼它將在狀態欄中顯示爲「離開」。

查詢

Select 
    id, date, present date 
    , CASE WHEN t2.id IS NULL THEN t1.Status ELSE ‘Leave’ END AS status 
from event table as t1 
left outer join leave table as t2 on 
    t1.id = t2.id and t1.date between t2.startdate and t2.enddate 

地,上述方法的工作,但我需要添加一個條件。

如果一旦我們申請的休假表的特定員工,那麼它應該比較當前日期列請假,如果當前日期欄是空的,那麼它應該顯示爲「離開」

期望輸出

ID Date PresentDate Status 

001 03/30/2010 03/30/2010 Present 
001 03/31/2010 null  absent 
001 04/01/2010 null  Leave 
001 04/02/2010 null  Leave 
001 04/03/2010 null  Leave (Expect this value) 
001 04/04/2010 04/04/2010 Present 
…. 

從上述輸出離開由04/01/2010開始04/02/2010,然後下一個當前日期的列是空值,則狀態應顯示爲「離開」,一旦當前日期不爲空,然後它應該顯示爲「Present」。

方法

We can display as "Leave" in status column from Start Date to end date of leave table, after that leave date end then we can compare with PresentDate column, if PresentDate column is null then it should display as "Leave", once data is available in present column then status should display with normal condition. 

如何使上述條件的查詢。

需要查詢幫助

回答

2
select E.id, E.date, E.presentdate, *, 
    case 
    when E.presentdate is not null then 'Present' 
    when E2.presentdate is not null then 'Absent' 
    when L.ID is not null then 'Leave' 
    else 'Absent' 
    end 
from Event E 
outer apply (
    select top 1 * 
    from Leave L 
    where E.presentdate is null and E.date >= L.startdate 
     AND e.ID = L.ID 
    order by L.startDate desc) L 
outer apply (
    select top 1 * 
    from Event E2 
    where E.presentdate is null 
     and E2.presentdate is not null 
     and E.date >= E2.date and E2.date > L.startdate  
     AND e2.ID = e.ID 
    order by E2.presentdate desc) E2 
order by E.date 

休假表

ID   StartDate    EndDate 
----------- ----------------------- ----------------------- 
1   2010-04-01 00:00:00.000 2010-04-02 00:00:00.000 
1   2010-04-02 00:00:00.000 2010-04-03 00:00:00.000 
1   2010-04-05 00:00:00.000 2010-04-05 00:00:00.000 

輸出

id   date     presentdate    
----------- ----------------------- ----------------------- ------- 
1   2010-03-30 00:00:00.000 2010-03-30 00:00:00.000 Present 
1   2010-03-31 00:00:00.000 NULL     Absent 
1   2010-04-01 00:00:00.000 NULL     Leave 
1   2010-04-02 00:00:00.000 NULL     Leave 
1   2010-04-03 00:00:00.000 NULL     Leave -** 
1   2010-04-04 00:00:00.000 2010-04-04 00:00:00.000 Present 
1   2010-04-05 00:00:00.000 NULL     Leave 
1   2010-04-06 00:00:00.000 NULL     Leave -** 
1   2010-04-07 00:00:00.000 NULL     Leave -** 
1   2010-04-08 00:00:00.000 2010-04-08 00:00:00.000 Present 
1   2010-04-09 00:00:00.000 NULL     Absent 
1   2010-04-10 00:00:00.000 NULL     Absent 
1   2010-04-11 00:00:00.000 2010-04-11 00:00:00.000 Present 

的那些標記 - **不受休假記錄覆蓋,但他們表現出離開,因爲他們遵循假期,正確?例如,2010-04-09保持「缺席」,因爲它遵循當前記錄(實際上不存在)。

+0

我有n個empid數,而且你的查詢也沒有比較事件表empid =離開表empid,它只比較日期。 – Gopal 2011-02-23 03:07:42