Consecutive Numbers


@zhouyanghhu I just tried your solution for the data below. it returns duplicated Nums. (1 1 1.). As the editorial solution suggested, 'distinct' is needed.
+++
 Id  Num 
+++
 1  1 
 2  1 
 3  1 
 4  1 
 5  1 
 6  2 
 7  2 
+++


Another approach using a nested query. I think it is better
set @prev = null;
set @conCount = 0;select distinct t.Num as ConsecutiveNums from
(select Num,
case
when @prev = Num then @conCount := @conCount + 1
when (@prev := Num) is not null then @conCount := 1
end as ConCount
from Logs
) as t
where t.ConCount = 3

@JoyJoe The description require the number that appears consecutively for at least three times，not just count it. So you have to solve the problem of consecutiveness.


select distinct W as ConsecutiveNums from (
select Num ,
@t:=case when @p1=Num and @p2=Num then Num else 999 end as W,
case when @t <> 999 then @p2:=2999 else @p2:=@p1 end,
case when @t <> 999 then @p1:=1999 else @p1:=Num end
from Logs ,(select @t:=0, @p1:=1999,@p2:=2999) a
) x
where x.W <> 999