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 > how to delete a duplicate data in a table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-13-07, 06:15
mabeljovan mabeljovan is offline
Registered User
 
Join Date: Jun 2007
Posts: 27
how to delete a duplicate data in a table

delete from table where exists (select lot_no,min(date) from table group by lot_no having count(*) > 1)

is this correct?
but i'm delete all the data in the table?
Reply With Quote
  #2 (permalink)  
Old 06-13-07, 06:40
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
If you run this:
Code:
select lot_no,min(date) from table group by lot_no having count(*) > 1)
Are the returned results what you want to delete? I don't think the min(date) bit is what you want.
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 06-13-07, 06:43
mabeljovan mabeljovan is offline
Registered User
 
Join Date: Jun 2007
Posts: 27
yes this is the result that i wanted to delete.
Reply With Quote
  #4 (permalink)  
Old 06-13-07, 06:44
mabeljovan mabeljovan is offline
Registered User
 
Join Date: Jun 2007
Posts: 27
because i have two records i want to keep the latest date so that is why i wanted it to be delete and i will use it for another complex query so that i why i don't want it to my table.
Reply With Quote
  #5 (permalink)  
Old 06-13-07, 06:59
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
try:

delete from table where (lot_no,date) in (select lot_no,min(date) from table group by lot_no having count(*) > 1);


but caution:
- if both rows have the same date, both will be deleted
- if there are more than two rows with same lot_no, only that with the oldest date will be deleted


<edit> maybe this is a better solution:

delete from table a where exists ( select * from table b where a.lot_no = b.lot_no and a.date < b.date )

</edit>

Last edited by umayer; 06-13-07 at 07:05.
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