Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 带两个条件的单查询_Sql_Sql Server_Sql Server 2005 - Fatal编程技术网

Sql 带两个条件的单查询

Sql 带两个条件的单查询,sql,sql-server,sql-server-2005,Sql,Sql Server,Sql Server 2005,使用SQLServer2005 离开桌子 ID StartDate EndDate 001 02/03/2010 02/03/2010 002 02/03/2010 null … 事件表 ID Date 001 02/03/2010 001 02/04/2010 001 02/05/2010 002 02/03/2010 002 02/04/2010 002 02/05/2010 …. 所有日期列数据类型都是datetime 我有n个身份证号码 我想创建一个状态列,比较事

使用SQLServer2005

离开桌子

ID StartDate EndDate 

001 02/03/2010 02/03/2010
002 02/03/2010 null
…
事件表

ID Date 

001 02/03/2010 
001 02/04/2010 
001 02/05/2010 
002 02/03/2010 
002 02/04/2010 
002 02/05/2010 
….
所有日期列数据类型都是
datetime

我有n个身份证号码

我想创建一个状态列,比较事件表中的日期和休假表中的结束日期

条件1

  • 如果特定id的休假表中有开始和结束日期,则该日期应在特定日期的事件表中显示为“休假”
质疑

条件2

  • 如果特定id的休假表中有开始日期,但没有结束日期,则在剩余日期的事件表中,该日期应显示为“休假”
质疑

预期产量

ID Date Status

001 02/03/2010  Leave
001 02/04/2010  
001 02/05/2010 
002 02/03/2010 Leave
002 02/04/2010 Leave
002 02/05/2010 Leave
….
上面的查询正在运行,但我想使用两个条件生成一个查询

如何查询上述条件


需要查询帮助

试试这个。如果在将来为leave表使用虚拟结束日期,则基本上是相同的查询

我选择2079年6月6日作为smalldatetime的最高值,但您可以根据需要更改此值

Select 
    id, date
    , CASE WHEN t2.id IS NULL THEN null ELSE ‘Leave’ END AS status 
from
   eventtable as t1 
   left outer join
   leave table as t2 on t1.id = t2.id and
          t1.date between t2.startdate and ISNULL(t2.enddate, '20790606')

也许这对你有用:

    Select 
    id, date
    , CASE WHEN t2.id IS NULL THEN null ELSE ‘Leave’ END AS status 
from event table as t1 
left outer join leave table as t2 on 
    t1.id = t2.id 
where (t1.date between t2.startdate and t2.enddate)
or (t2.enddate is null and (t1.date > t2.startdate))

我认为在日期中使用“或”条件可能会成为性能问题(索引使用等)

也许一个工会(或全体工会)会更有效:

Select 
    id, date
    , CASE WHEN t2.id IS NULL THEN null 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

Union

Select 
        id, date, 
        , CASE WHEN t2.id IS NULL THEN null 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 > t2.startdate
    Select 
    id, date
    , CASE WHEN t2.id IS NULL THEN null ELSE ‘Leave’ END AS status 
from event table as t1 
left outer join leave table as t2 on 
    t1.id = t2.id 
where (t1.date between t2.startdate and t2.enddate)
or (t2.enddate is null and (t1.date > t2.startdate))
Select 
    id, date
    , CASE WHEN t2.id IS NULL THEN null 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

Union

Select 
        id, date, 
        , CASE WHEN t2.id IS NULL THEN null 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 > t2.startdate