In PL/SQL, how do you update a row based on the next row?

后端 未结 5 1152
轮回少年
轮回少年 2021-01-18 10:20

I\'m using Oracle PL/SQL.

I have a timestamped table T, and I want to set a row\'s value for column A to be the same as that of the previous row, if they\'re sorted

5条回答
  •  温柔的废话
    2021-01-18 10:59

    What you can do is.

    update t
    set colToUpdate = nextValue
    from  (
    select A
          ,B
          ,C
          ,(LEAD(B, 1, null) over (order by A)) as nextValue
      FROM db.schema.table
      ) as t
        where colToUpdate is null
    

    This requires that the column you want to update is null, unless you want to update all of them.

提交回复
热议问题