lag()函数:
查询当前行向上偏移n行对应的结果
该函数有三个参数:第一个为待查询的参数列名,第二个为向上偏移的位数,第三个参数为超出最上面边界的默认值。
lead()函数:与上面相反
例题:查询连续出现的三次的数字
select
distinct num as ConsecutiveNums
from
(
select
id,num,
# 从当前记录获取前一行记录的num值,如果没有前一行,则返回null
lag(num,1,null) over (order by id) as lag_num,
# 从当前记录获取后一行记录的num值,如果没有后一行,则返回null
lead(num,1,null) over (order by id) as lead_num
from logs
) as l
# 当前值等于前一行的值,以及等于后一行的值,即满足了至少出现三次
where l.num = l.lag_num and l.num = l.lead_num
标签:函数,lead,lag,一行,num,sql,小众,null,id
From: https://www.cnblogs.com/zfx5201314/p/18045874