You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Leetcode SQL题Consecutive Numbers:未通过查询代码错误排查

LeetCode 连续出现的数字问题SQL错误排查

这是LeetCode上的「连续出现的数字」SQL题,要求从Logs表中查询出所有至少连续出现三次的数字。

Logs表结构

Column NameType
idint
numvarchar

id是本表的主键。

你的提交代码存在2处错误:

  • 表名引用错误:你定义的两个CTE分别为temp1、temp2,但最终查询语句写的是from temp,不存在名为temp的表,会直接触发语法错误
  • 结果重复问题:如果同一个数字有多段满足连续出现≥3次的序列,当前逻辑会返回重复的num,需要对最终结果去重

修正后的SQL代码

with temp1 as
(select num, 
       id, 
       row_number () over (partition by num order by id) as r
from logs),  
temp2 as
(select (id-r) as rn, num, count(num)  
from temp1
group by rn, num
having count(num)>=3)
select distinct num as ConsecutiveNums
from temp2

内容的提问来源于stack exchange,提问作者Ashima

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 09:54:06