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

后端 未结 5 1150
轮回少年
轮回少年 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 11:02

    And another option... doesn't quite do what do want because it ignores the requirement to sort on B but it might give you something to think about.... Without table definitions and things it was a little hard to get a handle on exactly what was required.

    Edit: on reading the question again, it looks like your syntax is wrong. Group functions (lead/lag/rank etc) can only appear in the select list or the order by clause. They are evaluated after the joins, where, group by and having clauses. So something like what is shown below should work.

    update T a
    set A = (select 
      new_A
      from (
      select 
        B, 
        A, 
        timestamp, 
        first_value(A) 
          over (order by timestamp range between 45 preceding and current row) as new_A
      from mike_temp_1
    ) b where b.id = a.id)
    

提交回复
热议问题