我有一个包含以下列的表:sID、start_date 和 end_date
其中一些值如下:
1 1995-07-28 2003-07-20
1 2003-07-21 2010-05-04
1 2010-05-03 2010-05-03
2 1960-01-01 2011-03-01
2 2011-03-02 2012-03-13
2 2012-03-12 2012-10-21
2 2012-10-22 2012-11-08
3 2003-07-23 2010-05-02
Run Code Online (Sandbox Code Playgroud)
我只想要结果中的第 2 行和第 3 行,因为它们是重叠的日期范围。
我试过这个,但它不会摆脱第一行。不知道我哪里出错了?
select a.sID from table a
inner join table b
on a.sID = b.sID
and ((b.start_date between a.start_date and a.end_date)
and (b.end_date between a.start_date and b.end_date ))
order by end_date desc
Run Code Online (Sandbox Code Playgroud)
我正在尝试在 SQL Server 中做