Group rows by contiguous date ranges for groups of values

前端 未结 1 1632
轻奢々
轻奢々 2021-01-22 01:03

Consider some table T, ordered by Col1, Col2, Date1, Date2:

Col1    Col2    Date1         Date2          rate
ABC     123     11/4/2014         


        
相关标签:
1条回答
  • You can identify the groups by using the difference of row_numbers(). Consecutive values will have a constant.

    select col1, col2, date1, min(date2), max(date2), rate
    from (select t.*,
                 (row_number() over (partition by col1, col2, date1 order by date2) -
                  row_number() over (partition by col1, col2, date1, rate order by date2)
                 ) as grp
          from table t
         ) t
    group by col1, col2, date1, rate, grp
    
    0 讨论(0)
提交回复
热议问题