If you just want to number them, I would suggest lag()with the total amount:
select t.*,
sum(case when datefrom = dateadd(day, 1, prev_dateto
then 0 else 1
end) over (order by itemId, datefrom)
from (select t.*,
lag(dateto) over (partition by itemid order by datefrom) as prev_dateto
from table t
) t;
casedetermines where the new island begins. The total amount simply summarizes this flag.
source
share