Based on your logic, dense_rank
won't do. However, you can get a similar effect with lag()
and a cumulative sum. The lag looks at the previous date value for the segment id/segment code, if they overlap, then the record is not the start of a grouping. Otherwise it is.
The cumulative sum of the "isstarts" is what you are looking for:
select t.*,
sum(case when date_from - prev_date_to < 0.00001
then 0
else 1
end) over (partition by segment_id order by SEGMENT_ID, SEGMENT_CODE
) as YourRanking
from (select t.*,
lag(date_to) over (partition by SEGMENT_ID, SEGMENT_CODE
order by date_from
) as prev_date_to
from t
) t
Because this is date/time, the query doesn't use exact equality. Instead it looks for a small difference.