- /*语法*/
- lag(exp_str,offset,defval) over() 取前
- Lead(exp_str,offset,defval) over() 取后
- --exp_str要取的列
- --offset取偏移后的第几行数据
- --defval:没有符合条件的默认值
eg1:
with tmp as(
select ‘1‘ id ,‘aa‘ name from dual union all
select ‘2‘ id ,‘bb‘ name from dual
union all
select ‘3‘ id ,‘cc‘ name from dual
)
select a.*,
lag(name,1) over (order by id asc) lag
from tmp a
运行结果是:
eg2
with tmp as(
select ‘1‘ id ,‘aa‘ name from dual union all
select ‘2‘ id ,‘bb‘ name from dual
union all
select ‘3‘ id ,‘cc‘ name from dual
)
select a.*,
lead(name,1) over (order by id asc) lag
from tmp a
时间: 2024-10-20 06:14:28