If I understand correctly, you want "islands" that are separated by 90 seconds. If so, you can use lag()
and a cumulative sum with aggregation:
select element, min(timestamp), max(timestamp), count(*)
from (select t.*,
sum(case when prev_timestamp > dateadd(second, -90, timestamp) then 0 else 1 end) over (partition by element order by timestamp) as grp
from (select t.*,
lag(timestamp) over (partition by element order by timestamp) as prev_timestamp
from t
) t
) t
group by grp, element;
Here is a db<>fiddle.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…