查找日期重叠的值

时间:2018-09-18 04:20:03

标签: sql-server date

我的SQL Server中有一个像这样的表:

ID Tblcd TblNo  Location    TableType   StartDate   EndDate
---------------------------------------------------------------
2   B      4      2B4          M        2/5/2014    4/6/2016
2   B      4      2B4          M        4/7/2017    NULL
2   B      4      2B4          K        3/4/2016    NULL
3   A      3      3A3          L        2/7/2018    7/4/2018
3   A      3      3A3          L        2/5/2018    NULL
1   C      5      1C5          N        2/4/2011    NULL
1   B      4      1B4          M        8/9/2015    3/5/2017

我想标识StartDate为2/5/2018的行,该行实际上与上一个条目重叠

尝试使用以下查询,但找不到第二项。我在这里想念什么。

SELECT
    A.Id AS AId,
    B.Id AS BId,
    A.StartDate AS AStartDate,
    A.EndDate AS AEndDate,
    B.StartDate AS BStartDate,
    B.EndDate AS BEndDate
FROM    
    dbo.TableDates AS a
INNER JOIN  
    dbo.TableDates AS b ON (b.Id = a.Id AND b.TblCd = a.TblCd AND b.TblNo = a.TblNo)
WHERE   
    a.StartDate <= b.EndDate
    AND a.EndDate >= b.StartDate

0 个答案:

没有答案