Вопрос

Here is an example of some of my data. I am trying to remove all extra data where the QueryID and RoyID are the same as other entries in the table, however, I would like to keep atleast one record. I'm not sure I'll be able to use the min or max functions on the ID column as some of the records have IDs like DEMO_12345 etc.

ID          QUERY_ID ROYALTY_ID
RTSQR1652   SQ1421  ROY25644005
RTSQR1653   SQ1421  ROY25636406
RTSQR1654   SQ1421  ROY25636557
RTSQR1655   SQ1421  ROY25636558
RTSQR1656   SQ1421  ROY25636559
RTSQR1657   SQ1421  ROY25636560

I was thinking to use ROWID instead of ID. Would the following query work?

 DELETE FROM RT_SOURCE_QUERY_ROYALTIES WHERE ROWID NOT IN (
 SELECT MAX(ROWID) FROM RT_SOURCE_QUERY_ROYALTIES GROUP BY ROYALTY_ID, QUERY_ID);
Это было полезно?

Решение

Just run a test

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;

Works as expected, doesn't it ...

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 

Другие советы

http://www.dba-oracle.com/t_delete_duplicate_table_rows.htm

Use self-join to delete duplicate rows

The most effective way to detect duplicate rows is to join the table against itself as shown below.

select 
   book_unique_id, 
   page_seq_nbr, 
   image_key 
from 
   page_image a 
where 
   rowid > 
     (select min(rowid) from page_image b 
      where 
         b.key1 = a.key1 
      and 
         b.key2 = a.key2 
      and 
         b.key3 = a.key3 
      );

Please note that you must specify all of the columns that make the row a duplicate in the SQL where clause.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top