Разобрался)
select distinct
id,
case
when
lag(dr) over(partition by id order by rn) is null
or lag(dr) over(partition by id order by rn) = dr
then
min(date_from) over(partition by id, dr order by rn)
else
min(date_from) over(partition by id, dr, rn order by rn)
end as date_from,
case
when
lead(dr) over(partition by id order by rn) is null
or lead(dr) over(partition by id order by rn) = dr
then
max(date_to) over(partition by id, dr order by rn DESC)
else
max(date_to) over(partition by id, dr, rn order by rn DESC)
end as date_to,
param1,
param2
from (
select
id, date_from, date_to, param1, param2,
row_number() over(partition by id order by date_from ASC) as rn,
dense_rank() over(partition by id order by param1, param2) as dr,
from
Table
) a1