LeetCode刷题实战180:连续出现的数字
Write an SQL query to find all numbers that appear at least three times consecutively.
Return the result table in any order.
题意
示例
解题
#对结果进行去重处理
select distinct Num as ConsecutiveNums
#将Num以及连续出现的次数作为一个新表
from (
#计算Num,到当前遍历的位置连续出现的次数
select Num, (
case
#如果Num == 前一个值,个数自增
when @prev = Num then @count := @count + 1
#否则计数器count为1,并更新prev为当前Num的值
when (@prev := Num) is not null then @count := 1
end
) as CNT
from Logs, (select @prev := null,@count := null) as t
) as temp
#筛选出连续出现次数超过3的
where temp.CNT >= 3
评论