October 15, 2004 at 1:22 am
Hi Guys,
I want to know how to delete duplicate records from a table in a single query in sql server.
Is it possible ??? How
Pls Help
October 15, 2004 at 3:35 am
Easiest way is to create a temporary table in which you insert all the records that are duplicates. Remember that this includes the records you want to keep as well. I.e two records for one that is duplicated. Insert the unique id and the value that you are checking for duplication. Use "having count > 1"
Then you can delete from the table where id in
(select max(id), duplicate
from table
group by duplicate).
This will then delete the records with the highest id. You can change this if you wish.
If you have records that have more than one duplicate, then you can put a loop in and check to see if there are any duplicates each time the loop starts.
If you have a search on this site, you will no doubt find a query already written that matches closely your requirement.
However, if you want a challenge..........
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply