问题是table:日志
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| num | varchar |
+-------------+---------+
id是该表的主键。编写一个SQL查询,查找至少连续出现三次的所有数字。
我的解决方案如下,Leetcode不接受。请帮我找出错误。
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 num as ConsecutiveNums
from temp
2条答案
按热度按时间apeeds0o1#
尝试:
演示:https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/174
LAG:https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html#function_lag
LEAD:https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html#function_lead
o7jaxewo2#