赞
踩
一、连续出现的数字
编写一个 SQL 查询,查找所有至少连续出现三次的数字。
返回的结果表中的数据可以按 任意顺序 排列。
查询结果格式如下面的例子所示:
方法一:
select distinct num as ConsecutiveNums
from (select num, id+1 - row_number() over(partition by num order by id) as diff
from Logs) t
group by num, diff
having count(*) >= 3;
方法二:
select distinct a.num as ConsecutiveNums
from logs as a
inner join logs as b
on a.id=b.id+1 and a.num=b.num
inner join logs as c
on a.id=c.id+2
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。