这会发现您的两行之间的重叠ID
等于 1:
declare @TabA table (ID int,EffDate datetime2,ExpDate datetime2)
insert into @TabA(ID,EffDate,ExpDate) values
(1,'20000101','20101231'),
(2,'20000101','20501231'),
(1,'20110101','20111231'),
(1,'20111231','20501231')
select
* --TODO -Pick columns
from
@TabA t1
inner join
@TabA t2
on
t1.ID = t2.ID and
t1.EffDate <= t2.ExpDate and --Play with these
t1.ExpDate >= t2.EffDate and --For Inclusive/Exclusive
t1.EffDate < t2.EffDate
结果:
ID EffDate ExpDate ID EffDate ExpDate
----------- ---------------------- ---------------------- ----------- ---------------------- ----------------------
1 2011-01-01 00:00:00.00 2011-12-31 00:00:00.00 1 2011-12-31 00:00:00.00 2050-12-31 00:00:00.00
您可以使用比较运算符来制作包含或排除边界值中的一个或EffDate
两个ExpDate
。