Question : Delete duplicates from table using dense_rank() in DB2

Hello all,

I haven't seen this subject in the context of DB2 so I thought I'd ask.  I'm trying to delete duplicates from a table within DB2 using dense_rank and columns col1, col2, col3, and col4.  I think i'm halfway there but need help in forming the delete portion of the statement:

These select statement shows me what I want, meaning that I see the duplicates according to the RANK_DATA column I've created (showing two 1's).  But from there I'm not quite sure how to isolate just one of those guys and delete it without deleting all instances of that row.  Any help is appreciated!

Thanks,
Glen
1:
2:
3:
4:
5:
6:
7:
select 
col1, 
col2, 
col3, 
col4,
dense_rank() over (order by col1, col2, col3, col4) as rank-data
from schema1.mytable with ur;

Answer : Delete duplicates from table using dense_rank() in DB2

Ok.  Now be sure and test this.  :)  (I don't have an iSeries box, so I can't)

DELETE FROM LIBA/mytable F1
WHERE RRN(F1) > (select MIN(RRN(F2)) FROM mytable F2 WHERE F1.col1=F2.col1 AND F1.col2=F2.col2 AND F1.col3=F2.col3 AND F1.col4=F2.col4 AND F1.col4='2005-01-01')



That should be pretty close...

Kent

Random Solutions  
 
programming4us programming4us