Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2007
    Posts
    27

    Unanswered: 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?

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    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
    Home | Blog

  3. #3
    Join Date
    Jun 2007
    Posts
    27
    yes this is the result that i wanted to delete.

  4. #4
    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.

  5. #5
    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 08:05.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •