繁体   English   中英

SQL Server 2012查找具有重叠日期的行

[英]SQL Server 2012 find rows with overlapping dates

我知道以前已经有人问过这个问题,但是我无法做出任何先前的答案,而且大多数答案都已经存在很多年了。 我有下表:

    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

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
             );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM