Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends,
I am using sql server 2014 version for storing data for my websites. Now I need to remove duplicate row from a sql table. So how can I do this?
Posted
Updated 13-Jan-16 5:23am
v2
Comments
Varun Sareen 13-Jan-16 7:11am    
What have you tried so far? And have you searched google?
ZurdoDev 13-Jan-16 7:21am    
DELETE FROM table
WHERE (conditions for duplicate)

WHERE are you stuck?
[no name] 13-Jan-16 11:12am    
I think the bigger problem is "DELETE except one of them" which leads to another "more difficult" condition
ZurdoDev 13-Jan-16 11:22am    
Right, but we don't know what duplicate means in this case. Which fields? All fields? Just some key ones?
Corporal Agarn 13-Jan-16 11:39am    
1) what is your table structure
2) what is your criteria for duplicate

Ultimately, these are the duplicate rows which we want to delete to resolve the issue. Use the following code to resolve it.


SQL
DELETE FROM dbo.ATTENDANCE WHERE AUTOID NOT IN (SELECT MIN(AUTOID) _
	FROM dbo.ATTENDANCE GROUP BY EMPLOYEE_ID,ATTENDANCE_DATE) 
Now check the data. No duplicate rows exist in the table.

Is it too complicated?
 
Share this answer
 
v2
A general solutions to your problem and preventing it in the future:

Having an identity field* in your table is a way to assure that you can never have two identical rows. Having such a field means you can uniquely select any row and do whatever you wish to that only that row. Problem will be gone 'forever'.

Now - in your current state - an easy solution would be to add an identity column to your table - then, find your rows which are otherwise duplicates, and delete all but one using whatever fields you wish as the search criteria to find functional duplicates.

More Preventive Medicine:
You could also define a UNIQUE constraint on as many columns as necessary and cause such entries to be rejected in advance. SQL UNIQUE Constraint[^]   - this adds overhead to your inserts and updates.


* a field with a default value of GetDate() is almost as good - but if the records are entered very quickly they could potentially have the same value. It's a larger data field, and, unless you've a need for a date the IDENTITY field is the way to go.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900