如何通过比较日期来建立状态

时间:2011-02-22 17:49:53

标签: sql sql-server sql-server-2005

使用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

在状态列中,如果Present Date为null,则它将显示为“absent”,如果不为null,则它将显示为“present”。现在,如果我们为日期申请休假,那么它将在状态栏中显示为“离开”。

查询

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,然后当前日期的下一列为空,然后状态应显示为“离开”,一旦出现日期不为空,那么它应该显示为“现在。

方法

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.

如何查询上述情况。

需要查询帮助

1 个答案:

答案 0 :(得分: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仍然是“缺席”,因为它遵循当前记录(实际上没有出现)。