如何从表中删除重复行

使用 rowid 伪列。
我们所要做的就是保留最新的数据(即最高的 ROWID)并删除其他重复的行。

SELECT * FROM table1 a
WHERE rowid < (SELECT max(rowid) FROM table1 b
WHERE a.column1 = b.column1 AND etc...);

或者

create table testtt (num number);
insert into testtt values(111);
insert into testtt values(111);
insert into testtt values(111);
insert into testtt values(111);
insert into testtt values(222);
insert into testtt values(222);
insert into testtt values(333);
insert into testtt values(333);
insert into testtt values(333);
select * from testtt;
delete from testtt
where (rowid, num) not in (select max_rid, num
from (select num,
count(num) over (partition by num) cnt,
max(rowid) over (partition by num) max_rid
from testtt)
where cnt > 1);
select * from testtt;

或者

虽然我怀疑这种方法比另一种方法有什么优势,但这是一个例子:

DELETE FROM table_a
WHERE rowid IN
( SELECT rowid FROM table_a
MINUS
SELECT MAX( rowid ) FROM table_a
GROUP BY column_list )

或者

从 table_name 中删除,其中 rowid 不在(从表组 byduplicate_values_field_name 中选择 max(rowid));

或者

最高的 rowid 并不一定意味着最新的数据......因为删除行释放的空间可能会被重用。

SQL> CREATE TABLE t AS SELECT level l FROM DUAL CONNECT BY LEVEL <= 5000;
SQL> DELETE FROM t WHERE l < 5000;
SQL> COMMIT;
SQL> INSERT INTO t VALUES (5001);
SQL> COMMIT;
SQL> SELECT max(l) KEEP(DENSE_RANK LAST ORDER BY rowid) as maxrid, max(l) KEEP(DENSE_RANK FIRST ORDER BY rowid) minrid FROM t;
MAXRID MINRID
---------- ---------
5000 5001
日期:2020-06-02 22:17:38 来源:oir作者:oir