Detect consecutive dates ranges using SQL

后端 未结 7 1191
南笙
南笙 2020-11-28 07:54

I want to fill the calendar object which requires start and end date information. I have one column which contains a sequence of dates. Some of the dates are consecutive (ha

相关标签:
7条回答
  • 2020-11-28 08:54

    Another simple solution that could work here is -

    with tmp as 
    (
    select
    datefield
    , dateadd('day',-row_number() over(order by date asc),datefield) as date_group 
    from table
    )
    select
    min(datefield) as start_date
    , max(datefield) as end_date 
    from tmp
    group by date_group
    
    0 讨论(0)
提交回复
热议问题