当前位置:   article > 正文

Sql刷题-力扣-中级(1)_在力扣如何刷sql

在力扣如何刷sql

一、连续出现的数字
编写一个 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;
  • 1
  • 2
  • 3
  • 4
  • 5

方法二:

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 
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/872022
推荐阅读