我尝试使用 CTE 和子查询,它们都给出与“删除的目标表结果不可更新”相同的错误。我尝试谷歌但没有找到有用的资源。
CTE代码
with Result as
(
select
*,
row_number() over(partition by id order by id) as RowNo
from
customers
)
delete from Result
where RowNo > 1;
子查询代码
delete Result
from (select *, row_number() over(partition by id order by id) as RowNo
from customers) Result
where Result.RowNo > 1;
图案:
DELETE t1
FROM table t1
JOIN table t2 ON t1.ident_column = t2.ident_column
AND t1.order_column < t2.order_column
或
DELETE
FROM table t1
WHERE EXISTS ( SELECT NULL
FROM table t2
WHERE t1.ident_column = t2.ident_column
AND t1.order_column < t2.order_column )
此模式通过
ident_column
仅保存组中 order_column
中具有最大值的行来删除重复的行。