当前位置:网站首页>Numbers appearing in a row [How to perfectly handle the problem of consecutive specific records?]
Numbers appearing in a row [How to perfectly handle the problem of consecutive specific records?]
2022-08-07 20:54:00 【REN_Linsen】
How to perfectly handle the continuous specific record problem?
前言
for continuous problems,在不采用SQL变量的情况下,It requires clever grouping(问题转换),Sort by a specific field,当序号id - 排序值rank 相等时,Indicates that two specific records are consecutive.
一、连续出现的数字


二、三种解法
1、violent couplet
select distinct l1.Num ConsecutiveNums
from Logs l1 join Logs l2 on l1.id + 1 = l2.id and l1.num = l2.num
join Logs l3 on l2.id + 1 = l3.id and l2.num = l3.num
2、Cleverly grouped
with view4row as(
select Id,num,
# 防止id不连续.
row_number() over() `row`
from Logs
),view4rank as(
select id,num,`row`,
# 注:rank()Waiting for sorting will mess up thatview的记录顺序.
rank() over(partition by num order by `row`) rk
from view4row
)
select distinct Num ConsecutiveNums
from view4rank
#`row` - rkEqual means innumon an equal basis,These are equalnum是连续的,This is in analysis How to group questions 的转换.
group by Num,`row` - rk
having count(1) > 2
3、加入变量
with view4base as(
select num,
case
when num = @pre then @cnt:=@cnt+1
when true then @cnt:=1
end cnt,
@pre:=num
from (select num from Logs) t1,(select @cnt:=1,@pre:='') t2 # Cartesian product hits the variable to the end of each record.
)
select distinct Num ConsecutiveNums
from view4base
where cnt > 2
总结
1)连续问题,It is necessary to target the specific fields of the requirements,Sort by their groupings,通过序号id - 排序值rankto group consecutive ones into groups.
2)In addition to the clever solution of problem transformation,It is also possible to assign variable fields to each row of records through a Cartesian product,通过操作变量,让SQL更灵活,以达到SQLprogramming effect.
参考文献
[1] LeetCode 连续出现的数字
边栏推荐
猜你喜欢

PHP将word文件转为图片预览

【LeetCode】1552. Magnetic force between two balls

116. Fill each node of the next node pointer on the right side - sequence traversal

《MySQL核心知识》第7章:插入、更新、删除
[C# language] DataGridView modify the color of the selected row

苹果怎么装双系统 苹果电脑双系统安装教程

解读三大财务报表
![[The Beauty of Software Engineering - Column Notes]](/img/4c/172e242abc6e80b51db9c3a35cf488.png)
[The Beauty of Software Engineering - Column Notes] "One Question and One Answer" Issue 4 | 14 Common Problem Solving Strategies for Software Development

主机基础设施如何使用 Zadig 做持续交付

偶像工场,开启虚拟人数字藏品新时代
随机推荐
【LeetCode】636.函数的独占时间
数学符号 参考手册大全
乡愁
Principles of PROFINET connection establishment
Mathematical Symbols Reference Manual
[C# language] DataGridView modify the color of the selected row
《MySQL核心知识》第7章:插入、更新、删除
【LeetCode】875.爱吃香蕉的珂珂
打印机怎么停止打印以前的任务 怎么取消打印机正在打印的文件
主机基础设施如何使用 Zadig 做持续交付
What is a split brain
Transformer pytorch implements line-by-line detailed explanation
Win7打开word提示“无法打开文件Normal因为内容有错误”怎么办
Concurrent foundation (2) : CPU multistage cache and cache consistency
Leetcode 剑指 Offer 20. 表示数值的字符串
datetime模块
Mathematics Remainder Theorem Introduction
【LeetCode】1552. Magnetic force between two balls
Linear Prediction and Autoregressive Modeling
可信认证之九阴真经一