简体   繁体   English

查找连续的开始/结束日期列之间的日期间隔

[英]Find Date Gap Between Consecutive Start/End Date Columns

I have a table called periodDefinition with columns rowNum, periodSeq, startDate, endDate. 我有一个名为periodDefinition的表,其中包含rowNum,periodSeq,startDate,endDate列。 I am trying to write a SQL query to find a gap between the endDate of one row and the startDate of the next row. 我试图编写一个SQL查询来查找一行的endDate和下一行的startDate之间的间隙。 Right now it's seeing a "gap" if the startDate of the next row isn't the exact same date as the endDate of the previous row. 现在,如果下一行的startDate与上一行的endDate的日期不完全相同,则会看到“空白”。

Most advice seems to be to create a calendar temp table and double check against that, but due to work constriction policies, I cannot do that. 大多数建议似乎是创建一个日历临时表并对此进行仔细检查,但是由于工作限制政策,我不能这样做。 Thanks for any advice you can give. 感谢您提供的任何建议。

Sample Data: 样本数据:

rowNum: 1, periodSeq: 1, startDate: 09/01/2014, endDate: 09/30/2014 rowNum:1,periodSeq:1,startDate:09/01/2014,endDate:09/30/2014

rowNum: 2, periodSeq: 2, startDate: 10/01/2014, endDate: 10/30/2014 rowNum:2,periodSeq:2,startDate:10/01/2014,endDate:10/30/2014

rowNum: 3, periodSeq: 4, startDate: 11/01/2014, endDate: 11/30/2014 rowNum:3,periodSeq:4,startDate:11/01/2014,endDate:11/30/2014

Test Query: 测试查询:

SELECT endDate, startDate
FROM
(
    SELECT DISTINCT startDate, ROW_NUMBER() OVER (ORDER BY startDate) RN
    FROM dbo.PeriodDefinition T1
    WHERE
        NOT EXISTS (
            SELECT *
            FROM dbo.PeriodDefinition T2
            WHERE T1.startDate > T2.startDate AND T1.startDate < T2.endDate
        )
    ) T1
JOIN (
    SELECT DISTINCT endDate, ROW_NUMBER() OVER (ORDER BY endDate) RN
    FROM dbo.PeriodDefinition T1
    WHERE
        NOT EXISTS (
            SELECT *
            FROM dbo.PeriodDefinition T2
            WHERE T1.endDate > T2.startDate AND T1.endDate < T2.endDate
        )
 ) T2
 ON T1.RN - 1 = T2.RN
WHERE
 endDate < startDate

Results: 结果:

endDate: 2014-09-30, startDate, 2014-01-01 endDate:2014-09-30,startDate,2014-01-01

endDate: 2014-09-30, startDate: 2014-11-01 结束日期:2014-09-30,开始日期:2014-11-01

endDate: 2014-10-30, startDate: 2014-12-01 结束日期:2014-10-30,开始日期:2014-12-01

Desired Results: 所需结果:

endDate: 2014-10-30, startDate: 2014-11-01 结束日期:2014-10-30,开始日期:2014-11-01

Assuming that rowNum is sequential with no gaps. 假设rowNum是连续的且没有间隙。

Here's a simple way to get your desired result: 这是获得所需结果的一种简单方法:

SELECT t1.EndDate, t2.StartDate
FROM PeriodDefinition t1
INNER JOIN PeriodDefinition t2
  ON t1.rowNum=t2.rowNum - 1
WHERE DATEDIFF(day, t1.EndDate, t2.StartDate)>1

If you are interested in getting the missing dates you could do something like this using a recursive cte. 如果您有兴趣获取缺少的日期,则可以使用递归cte执行以下操作。

WITH cte AS (
    SELECT  MIN(startDate) startDate,
            MAX(startDate) endDate
    FROM    SampleData
    UNION ALL
    SELECT  DATEADD(day, 1, startDate),
            endDate
    FROM    cte 
    WHERE   DATEADD(day, 1, startDate) <= endDate
)

SELECT  cte.startDate 
FROM    cte
        LEFT JOIN SampleData s ON cte.startDate BETWEEN s.startDate AND s.endDate
WHERE   s.startDate IS NULL 
OPTION (MAXRECURSION 0);

SQL Fiddle SQL小提琴

If you just want to use a query similar to yours, you can just use 如果您只想使用与您的查询类似的查询,则可以使用

SELECT
    endDate,
    startDate
FROM
    (SELECT startDate,
            ROW_NUMBER () OVER (ORDER BY startDate) RN 
     FROM   PeriodDefinition T1
    ) T1
    JOIN (SELECT endDate,
                 ROW_NUMBER () OVER (ORDER BY endDate) RN 
          FROM PeriodDefinition T1
    ) T2 ON T1.RN - 1 = T2.RN
WHERE
    endDate + 1 < startDate

SQL Fiddle SQL小提琴

the problem with your query, that I saw, was that you were not adding 1 to the endDate in your where 我看到的查询问题是您没有在您的where的endDate中添加1

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

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