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.
如何使上述條件的查詢。
需要查詢幫助
我有n個empid數,而且你的查詢也沒有比較事件表empid =離開表empid,它只比較日期。 – Gopal 2011-02-23 03:07:42