基于子查询的更新失败

Obi*_*obi 1 sql oracle analytics

我试图在Oracle 10gR2中进行以下更新:

update
  (select voyage_port_id, voyage_id, arrival_date, port_seq,
    row_number() over (partition by voyage_id order by arrival_date) as new_seq
   from voyage_port) t
set t.port_seq = t.new_seq
Run Code Online (Sandbox Code Playgroud)

Voyage_port_id是主键,voyage_id是外键.我正在尝试根据每次航行中的日期分配序列号.

但是,上述操作因ORA-01732而失败:数据操作操作在此视图中不合法

有什么问题,如何避免呢?

And*_*mar 5

由于无法使用更新子查询row_number,因此必须计算set更新部分中的行号.起初我试过这个:

update voyage_port a
set a.port_seq = (
  select 
    row_number() over (partition by voyage_id order by arrival_date)
  from voyage_port b
  where b.voyage_port_id = a.voyage_port_id
)
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为子查询只选择一行,然后row_number()始终为1.使用另一个子查询可以得到有意义的结果:

update voyage_port a
set a.port_seq = (
  select c.rn
  from (
      select 
        voyage_port_id
      , row_number() over (partition by voyage_id 
            order by arrival_date) as rn
      from voyage_port b
   ) c
  where c.voyage_port_id = a.voyage_port_id
)
Run Code Online (Sandbox Code Playgroud)

它有效,但比我对这项任务的期望更复杂.