Consider some table T
, ordered by Col1, Col2, Date1, Date2
:
Col1 Col2 Date1 Date2 rate
ABC 123 11/4/2014
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