Saturday, January 21, 2006

oracle - removing duplicate records

Question:

I have a table T1 with columns C1, C2 and C3. Currently, there are duplicate
records in T1 with the C1, C2 combination.
T1:
C1 C2 C3
-- -- --
1 50 a
1 50 b
2 89 x
2 89 y
2 89 z

I would like to delete the duplicate record on this combination. What is the best way of writing the PL/SQL block or query to
do this?

Answer:

delete from T1
2 where rowid <> ( select max(rowid)
3 from t1 b
4 where b.c1 = t1.c1
5 and b.c2 = t1.c2 )
6 /

3 rows deleted.


select * from t1;

C1 C2 C
---------- ---------- -
1 50 b
2 89 z

No comments: