Oracle处理当前行与其之前行

平移: lag/lead over 函数

  1. oralce内置的高级函数,可以获取到当前查询结果集中每一行的前几行,相当于做了垂直方向的平移
  2. 要注意的函数作用域是此次查询结果集,虽然提供了分组的功能,只是对当前结果集的分组
  3. lagover
  4. sample
with tmp as(
select '1' id ,'aa' name ,'19' age from dual union all
select '31' id ,'aa' name ,'31' age from dual union all
select '2' id ,'bb' name ,'20' age from dual union all
select '3' id ,'CC' name ,'21' age from dual
)
select a.*,
       lag(age,1) over (order by name, id asc) lag,
      a.age - lag(age,1) over (order by id asc) lag1
from tmp a;

这里写图片描述

填充: 关联

  1. 通过利用rownum做一次逆序排列,对null值用其之前不为0的数值填充
  2. with a as (), b as () select * from a; 缓存表数据
  3. sample
with test as (
select rownum rn, id, age from(
select '1' id , 19 age from dual union all
select '2' id , null age from dual union all
select '3' id , null age from dual union all
select '4' id , 21 age from dual union all
select '5' id , 0 age from dual  )
),
test2 as (select * from test order by rn desc)
select tm.id, tm.age, 
  ( select t2.age from test2 t2 
  where t2.rn <= tm.rn and t2.age > 0 and rownum = 1) agepad
      from test tm;

这里写图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值