我有一个包含 15 个重复行的表(总共 30 个,包括原始行 + 重复行)。 我的任务是编写一个脚本来获取重复记录并删除它们。 我的问题是,该表不包含像“shipment_item_id”这样的 ID 列。 所以我找到了一个解决方案,使用 rowid 添加我自己的 id 列,然后我可以区分受骗者和原始者。
我编写了一个获取重复项的 SELECT 语句,但是当我尝试将其包含在 DELETE 语句中时,出现错误“ORA-00913 - 值太多”。 我很难相信 15 条记录太多了。
这是脚本。 我确信这是一个简单的解决方法,但我一直无法弄清楚:
DELETE
FROM SHIPMENT_ITEM
WHERE rowid not in (
SELECT max(rowid),
po_number,
item_number
FROM shipment_item
GROUP BY
po_number,
item_number
HAVING COUNT(*) > 1
)
DELETE
FROM SHIPMENT_ITEM
WHERE rowid not in ( --> you are comparing ROWID with ...
SELECT max(rowid), --> ... max(rowid), but also ...
po_number, --> ... po_number and ...
item_number --> ... item_number
FROM shipment_item
GROUP BY
po_number,
item_number
HAVING COUNT(*) > 1
)
从子查询中删除多余的列!
DELETE
FROM SHIPMENT_ITEM
WHERE rowid not in (
SELECT max(rowid)
FROM shipment_item
GROUP BY
po_number,
item_number
HAVING COUNT(*) > 1
)