Saya akan menganggap bahwa id
unik dan meningkat. Anda bisa mendapatkan jumlah nilai berurutan dengan menggunakan nomor baris yang berbeda. Berikut ini menghitung semua urutan:
select grp, value, min(id), max(id), count(*) as cnt
from (select t.*,
(row_number() over (order by id) - row_number() over (partition by value order by id)
) as grp
from table t
) t
group by grp, value;
Jika Anda ingin urutan 0s terpanjang:
select top 1 grp, value, min(id), max(id), count(*) as cnt
from (select t.*,
(row_number() over (order by id) - row_number() over (partition by value order by id)
) as grp
from table t
) t
group by grp, value
having value = 0
order by count(*) desc