SQL Find missing date ranges

前端 未结 2 1437
情深已故
情深已故 2021-01-03 11:23

I have a table that holds all the days/months of the year

E.G.

Day      Month
1         9
2         9
3         9
4         9
5         9
6         9         


        
2条回答
  •  走了就别回头了
    2021-01-03 11:49

    You can use CTE and write a query as:

    declare @StartDate DATE, @EndDate DATE
    set @StartDate = '2013-09-01';
    set @EndDate = '2013-09-30';
    
      WITH DateRange(Date) AS
         (
             SELECT
                 @StartDate Date
             UNION ALL
             SELECT
                 DATEADD(day, 1, Date) Date
             FROM
                 DateRange
             WHERE
                 Date < @EndDate
         )
    
         SELECT 'webshop',Date 
         FROM DateRange
         EXCEPT 
         SELECT DataSet,DateRange
         FROM ImportedDateRange
         WHERE DataSet='webshop'
         --You could remove Maximum Recursion level constraint by specifying a MaxRecusion of zero
         OPTION (MaxRecursion 10000);
    

提交回复
热议问题