Je vais supposer que id
est unique et en augmentation. Vous pouvez obtenir le nombre de valeurs consécutives en utilisant les différents numéros de ligne. Ce qui suit compte toutes les séquences :
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;
Si vous voulez la séquence de 0 la plus longue :
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