यह अंतराल और द्वीपों का एक रूप है। द्वीपों को प्राप्त करने के लिए आप पंक्ति संख्याओं के अंतर का उपयोग कर सकते हैं:
select device_id, speed, count(*) as num_times
from (select t.*,
row_number() over (partition by device_id order by datetime) as seqnum,
row_number() over (partition by device_id, speed order by datetime) as seqnum_s
from t
) t
group by device_id, speed, (seqnum - seqnum_s);
फिर, अधिकतम प्राप्त करने के लिए, विंडो फ़ंक्शंस की एक और परत का उपयोग करें:
select device_id, speed, num_times
from (select device_id, speed, count(*) as num_times,
row_number() over (partition by device_id order by count(*) desc) as seqnum
from (select t.*,
row_number() over (partition by device_id order by datetime) as seqnum,
row_number() over (partition by device_id, speed order by datetime) as seqnum_s
from t
) t
group by device_id, speed, (seqnum - seqnum_s)
) ds
where seqnum = 1;