Leetcode SQL题Consecutive Numbers:未通过查询代码错误排查
LeetCode 连续出现的数字问题SQL错误排查
这是LeetCode上的「连续出现的数字」SQL题,要求从Logs表中查询出所有至少连续出现三次的数字。
Logs表结构
| Column Name | Type |
|---|---|
| id | int |
| num | varchar |
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
相关产品推荐
相关产品推荐

