力扣180--oracle
Create table Logs (id int, num int); insert into Logs (id, num) values (1, 1); insert into Logs (id, num) values (2, 1); insert into Logs (id, num) values (3, 1); insert into Logs (id, num) values (4, 2); insert into Logs (id, num) values (5, 1); insert into Logs (id, num) values (6, 2); insert into Logs (id, num) values (7, 2);
编写一个 SQL 查询,查找所有至少连续出现三次的数字,返回的结果表中的数据可以按 任意顺序 排列,期望得到的结果
答案
with t as (select id, num, row_number() over(order by id) - row_number() over(partition by Num order by id) as gp from logs) select distinct num as ConsecutiveNums from t group by gp, num having count(num) >= 3
理解下面代码的输出结果