我有2个带有公共标识符列的表。 将第一个tab1与tab2进行比较,并需要日期不在tab2之间的结果
Create table #tab1(id int, Idvalue int, IDDate date)
insert into #tab1
select 1,1, '2013-06-25' UNION ALL
select 2,1, '2014-11-28' union all
select 3,1, '2015-12-06' union all
select 4,1, '2013-04-08' union all
select 5,1, '2051-12-12'
Select * from #tab1
Create table #tab2(Idvalue int, Startdate date, EndDate Date)
insert into #tab2
select 1, '2013-05-01','2013-12-31' UNION ALL
select 1, '2014-06-01','2050-01-01'
Select * from #tab2
DROP table #tab1
DROP table #tab2
结果:
4 1 2013-04-08
5 1 2051-12-12
答案 0 :(得分:1)
不存在的版本:
select *
from #tab1
where not exists
(
select 1
from #tab2
where #tab1.IDDate between #tab2.Startdate and #tab2.EndDate
)
答案 1 :(得分:0)
尝试左联接
select * from tab1 left join tab2 on IDDate between Startdate and enddate
where Idvalue is null
答案 2 :(得分:0)
我不确定,但是你可以加入
SELECT *
FROM tab1
LEFT JOIN tab2
ON tab1.idvalue = tab2.idvalue
AND tan1.iddate > tab2.start
AND tab1.iddate < tab2.end
希望有帮助
答案 3 :(得分:0)
这应该可行,或者可能需要对IN_Data_Range列进行一些其他过滤:
SELECT *
,CASE
WHEN IDDate BETWEEN StartDate
AND EndDate
THEN 'YES'
ELSE 'NO'
END AS IN_Data_Range
FROM #tab1
CROSS APPLY #tab2