使用SQL Sever 2005
表1
ID StartDate EndDate
001 02/23/2010 07/22/2010
002 05/03/2010 null
003 02/02/2011 null
...
表2
日期
02/24/2011
02/25/2011
...
...
条件
enddate
不为空,则startDate
与来自table2的max(date)进行比较,如果startdate> 6个月后它应显示为“已过期”如何查询上述情况。
需要查询帮助。
答案 0 :(得分:2)
Select
StartDate,
[Status]=Case
when DATEDIFF(m,StartDate,(Select MAX(Date) from Table2))>6
then 'Expired'
Else 'Valid'
end
From Table1
where EndDate is not null
答案 1 :(得分:0)
Select Case
When EndDate Is Not Null Then EndDate
When Table1.StartDate > DateAdd(mm, 6, T2.MaxDate) Then 'Expired'
End
From Table1
Cross Join (
Select Max( [Date] ) As MaxDate
From Table2
) As T2
答案 2 :(得分:0)
问题不完整
但我们假设你的意思是
enddate < max(date)
然后查询变为
select
case when EndDate < MaxDate then 'Expired'
when EndDate is null and StartDate < DateAdd(m,-6,GetDate()) then 'Expired'
else 'OK'
end
from Table1 T1
cross join (select max(date) MaxDate from Table2) T2