题目如下:
Write a SQL query to find all numbers that appear at least three times consecutively.
+----+-----+ | Id | Num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+
For example, given the above Logs
table, 1
is the only number that appears consecutively for at least three times.
发现网上的大多数思路都用到了自定义变量,并且比较麻烦
个人解题的时候用了个土办法。
如下:
select distinct Num as ConsecutiveNums from Logs as l
where l.num =
(select num from Logs as p where l.id+2=p.id )
&&
l.num =
(select num from Logs as p where l.id+1=p.id )
简单粗暴- -。判断连续三个是否num相等。