If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Need to delet duplicate and keep two duplicate records

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-23-11, 02:52
colloquy84 colloquy84 is offline
Registered User
 
Join Date: Mar 2008
Posts: 12
Need to delet duplicate and keep two duplicate records

Hi All,

I have a table which has duplicate rows. I need to delete the duplicate rows but I can not simply delete all the the duplicate rows and keep only one copy of the record. My requirment is such that, I need to delete Duplicate rows but need to keep two duplicate rows.

For example If I have a table Employee with the records as shown below.

Employee
-------------------------------
First_Name Last_Name
Arun Pandey
Arun Pandey
Arun Pandey
Arun Pandey
Arun Pandey
Arun Pandey
Arun Pandey
Shweta Tiwari
Shweta Tiwari
Shweta Tiwari
Shweta Tiwari
Shweta Tiwari

After running the delete query I should have the below values in the table.

Employee
-------------------------------
First_Name Last_Name
Arun Pandey
Arun Pandey
Shweta Tiwari
Shweta Tiwari

I am not able to think of any query which can give me this result. I need help on this.

Thanks
Arun
Reply With Quote
  #2 (permalink)  
Old 06-23-11, 03:18
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
I think my Oracle solution should work on DB2 as well (using DB2's rid_bit() instead of rowid)
Code:
DELETE FROM employee
WHERE rid_bit() IN (SELECT rb
                    FROM (
                      SELECT rid_bit() as rb, 
                             row_number() over (partition by first_name, last_name order by first_name) as counter
                      FROM employees
                   ) 
                   WHERE counter > 2)
Reply With Quote
  #3 (permalink)  
Old 06-23-11, 03:26
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Another example:

Code:
DELETE
 FROM  (SELECT ROW_NUMBER()
                  OVER(PARTITION BY First_Name , Last_Name) rn
         FROM  Employee
       )
 WHERE rn > 2
;

Last edited by tonkuma; 06-23-11 at 03:39. Reason: Removed First_Name , Last_Name in SELECT list.
Reply With Quote
  #4 (permalink)  
Old 06-23-11, 03:37
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Cool. Didn't know this was possible in DB2
Reply With Quote
  #5 (permalink)  
Old 06-23-11, 04:05
colloquy84 colloquy84 is offline
Registered User
 
Join Date: Mar 2008
Posts: 12
Thanks for the reply. It really solved my issue. Thanks once again.

Arun
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On