更新时标识符无效 (Oracle)

Invalid Identifier on Update (Oracle)

我尝试执行以下命令:

UPDATE DB_TEST.STOCK_ITEMS
   SET STATUS = (SELECT *
                   FROM (SELECT STOCK_ITEM_STATUS
                           FROM DB_TEST.STOCK_ITEMS_HISTORY
                          WHERE STOCK_ITEM_ID = DB_TEST.STOCK_ITEMS.ID
                          ORDER BY CHANGED_ON DESC, ID DESC)
                  WHERE ROWNUM <= 1)
 WHERE EXISTS (SELECT *
                 FROM DB_TEST.STOCK_ITEMS_HISTORY
                WHERE STOCK_ITEM_ID = DB_TEST.STOCK_ITEMS.ID);

但我收到错误消息:

SQL-Error: ORA-00904: "DB_TEST"."STOCK_ITEMS"."ID": invalid identifier

我查找了 Oracle 错误,但我得到的只是我应该使用了错误或缺失的列名,但 DB_TEST.STOCK_ITEMS.ID 字段确实存在。

还有哪些其他原因会导致此错误?

Oracle 将 table 的范围限制为一级子查询。这是使用 keep:

解决问题的方法
UPDATE DB_TEST.STOCK_ITEMS
  SET STATUS = (SELECT MAX(STOCK_ITEM_STATUS) KEEP (DENSE_RANK FIRST ORDER BY CHANGED_ON DESC, ID DESC)
                FROM DB_TEST.STOCK_ITEMS_HISTORY
                WHERE STOCK_ITEM_ID = DB_TEST.STOCK_ITEMS.ID
               )
  WHERE EXISTS (SELECT 1
                FROM DB_TEST.STOCK_ITEMS_HISTORY
                WHERE STOCK_ITEM_ID = DB_TEST.STOCK_ITEMS.ID
               );