What is a good way to find gaps in a set of datespans?

前端 未结 3 1134
清酒与你
清酒与你 2021-01-18 19:38

What is a way to find gaps in a set of date spans?

For example, I have these date spans:

1/ 1/11 - 1/10/11  
1/13/11 - 1/15/11  
1/20/11 - 1/30/11
         


        
3条回答
  •  伪装坚强ぢ
    2021-01-18 20:36

    I want to be able to tell that between 1/10/11 and 1/13/11 there is a gap so the start and end date is not possible.

    I think you're asking this question: does the data in your table have a gap between the start date and the end date?

    I created a one-column table, date_span, and inserted your date spans into it.

    You can identify a gap by counting the number of days between start date and end date, and comparing that the the number of rows in date_span for the same range.

    select 
      date '2011-01-14' - date '2011-01-07' + 1 as elapsed_days,  
      count(*) from date_span 
    where cal_date between '2011-01-07' and '2011-01-14';
    

    returns

    elapsed_days count    
    --           --
    8            6
    

    Since they're not equal, there's a gap in the table "date_span" between 2011-01-07 and 2011-01-14. I'll stop there for now, because I'm really not certain what you're trying to do.

提交回复
热议问题