删除重复的行 - Oracle

use*_*258 1 oracle plsql duplicate-removal rowid

这是我的一些数据的示例.我试图删除所有额外的数据,其中QueryID和RoyID与表中的其他条目相同,但是,我想保留至少一条记录.我不确定我是否能够在ID列上使用min或max函数,因为某些记录具有DEMO_12345等ID.

ID          QUERY_ID ROYALTY_ID
RTSQR1652   SQ1421  ROY25644005
RTSQR1653   SQ1421  ROY25636406
RTSQR1654   SQ1421  ROY25636557
RTSQR1655   SQ1421  ROY25636558
RTSQR1656   SQ1421  ROY25636559
RTSQR1657   SQ1421  ROY25636560
Run Code Online (Sandbox Code Playgroud)

我在考虑使用ROWID而不是ID.以下查询是否有效?

 DELETE FROM RT_SOURCE_QUERY_ROYALTIES WHERE ROWID NOT IN (
 SELECT MAX(ROWID) FROM RT_SOURCE_QUERY_ROYALTIES GROUP BY ROYALTY_ID, QUERY_ID);
Run Code Online (Sandbox Code Playgroud)

Rob*_*ebe 7

只是进行一次测试

drop table test1;

create table test1 (a number,b number, c number);

insert into test1 values (1,1,2);
insert into test1 values (1,1,3);
insert into test1 values (1,2,2);
insert into test1 values (2,1,2);
insert into test1 values (2,2,2);
insert into test1 values (1,1,2);

select * from test1;

delete from test1 where rowid not in (select max(rowid) from test1 group by a,b);

select * from test1;
Run Code Online (Sandbox Code Playgroud)

按预期工作,不是吗......

table TEST1 dropped.
table TEST1 created.
1 rows inserted.
1 rows inserted.
1 rows inserted.
1 rows inserted.
1 rows inserted.
1 rows inserted.
A B C
- - -
1 1 2 
1 1 3 
1 2 2 
2 1 2 
2 2 2 
1 1 2 

 6 rows selected 

2 rows deleted.
A B C
- - -
1 2 2 
2 1 2 
2 2 2 
1 1 2 
Run Code Online (Sandbox Code Playgroud)

  • rowid(不是rownum)与'记录顺序'无关,它是记录存储位置的技术表示.OP只想保留一条记录,其中一条未指定,并不重要. (2认同)