oracle取上一行,请教SQL取某一标识的上一行如何取?

SQL> create table t_eli(user_id int,  step int, tag varchar2(30));

Table created

SQL> select * from T_ELI;

USER_ID                                    STEP TAG

--------------------------------------- --------------------------------------- ------------------------------

123456                                       1 0

123456                                       2 xxx

123456                                       3 abc

123456                                       4 right

321654                                       1 xxx

321654                                       2 ggg

321654                                       3 sds

321654                                       4

321654                                       5 right

9 rows selected

SQL>

SQL> select *

2    from t_eli

3   where tag = 'right'

4   union all

5  select b.user_id,c.step,c.tag

6  from (

7  select user_id,last_step

8    from (

9  select user_id,tag,lag(step) over(partition by user_id order by step) last_step from t_eli

10  ) a

11  where a.tag = 'right') b,t_eli c

12  where b.user_id = c.user_id

13    and b.last_step = c.step

14  order by 1,2,3

15  /

USER_ID                                    STEP TAG

--------------------------------------- --------------------------------------- ------------------------------

123456                                       3 abc

123456                                       4 right

321654                                       4

321654                                       5 right

SQL>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值