【问题标题】:Update current row using previous row's value in Oracle使用 Oracle 中上一行的值更新当前行
【发布时间】:2016-03-29 09:33:11
【问题描述】:

我有一个名为 batch_history 的表,其中包含以下数据。该表包含不同批次的数据(批次 ID 会有所不同)。我想将 current_status_start_date 更新为上一行的 rec_change_date。我希望仅对 current_status_id 为 3310 和 3320 的记录进行更新。这应该对 batch_history 表中 data_stream_id 为 129 的所有批次(在屏幕截图中我刚刚显示一个批次的数据)进行。

batch_history 表中的当前数据:

batch_history 表中需要的输出:

我已经使用“WITH”子句进行了查询,但我仍然无法弄清楚这个问题的确切用法和解决方案。请帮忙。提前致谢。

【问题讨论】:

标签: oracle oracle11g


【解决方案1】:

你可以这样做:

UPDATE batch_history t  
SET t.current_status_start_date = NVL((SELECT max(s.rec_change_date) FROM batch_history s
                                       WHERE s.rec_change_date < t.rec_change_date 
                                         and t.batch_id = s.batch_id),
                                      current_status_start_date )

【讨论】:

  • 谢谢萨吉。它正在用下​​一行的 rec_change_date 更新 current_status_id。但是,我需要它来更新前一行的值。
  • @prabuR 哦,搞混了..现在试试:)
猜你喜欢
  • 1970-01-01
  • 2019-07-06
  • 2021-12-16
  • 2013-01-10
  • 1970-01-01
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多