SQL Server 2012查找日期重叠的行
我知道以前有人问过这个问题,但我不能让以前的答案对我有用,而且大多数答案都是多年前的。我有下表:SQL Server 2012查找日期重叠的行,sql,sql-server,Sql,Sql Server,我知道以前有人问过这个问题,但我不能让以前的答案对我有用,而且大多数答案都是多年前的。我有下表: ID Start End 1 1/1/2018 1/14/2018 1 1/15/2018 1/30/2018 1 1/20/2018 2/5/2018 2 2/1/2018 2/4/2018 2 2/1/2018 2/4/2018 2 2/2
ID Start End
1 1/1/2018 1/14/2018
1 1/15/2018 1/30/2018
1 1/20/2018 2/5/2018
2 2/1/2018 2/4/2018
2 2/1/2018 2/4/2018
2 2/20/2018 2/25/2018
3 2/1/2018 2/15/2018
我需要能够看到我的结果中按ID有重叠日期段的行。这些行将出现在我的结果中:
ID Start End
1 1/15/2018 1/30/2018
1 1/20/2018 2/5/2018
2 2/1/2018 2/4/2018
2 2/1/2018 2/4/2018
您可以使用exists:
您可以使用exists:
这给了我结果中的所有8行,而不仅仅是重叠的行…?我只需要结果中的重叠行。我应该只有4行。@SheriAnderson。你的问题没有说明这一点,但我猜测你希望在给定的id内重叠。这给了我结果中的所有8行,而不仅仅是重叠行…?我只需要结果中的重叠行。我应该只有4行。@SheriAnderson。您的问题没有说明,但我猜测您希望在给定id内重叠。
select t.*
from t
where exists (select 1
from t t2
where t2.id = t.id and t2.start < t.end and t2.end > t.start
);