Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to delete redundant data from a table
I have a table t1
---------------------
ID Name
---------------------
1 A
2 B
3 C
4 D
3 C
2 B

I want to delete redundant data using sql query.

OUTPUT will be
VB
---------------------
ID      Name
---------------------
1   A
2   B
3   C
4   D
Posted
Comments
swapnilKumbhar 13-Apr-12 8:37am    
Why don't you use primary key while inserting Record?
[no name] 13-Apr-12 8:47am    
Or, even better, add a unique constraint on the column 'Name' so that duplicate values can't be inserted into the table in the first place.

You can use the following SQL query to delete duplicate records:
SQL
DELETE FROM t1 WHERE ID NOT IN (SELECT MAX(ID) FROM t1 GROUP BY [name])
 
Share this answer
 
Comments
P.S Vijay 13-Apr-12 7:57am    
It is not working
There are no. of ways.
I used:
you can make a temporary table with primary key with ID and Name.
and paste these records into temp table.
your temp table now have distict record.
Other:
http://support.microsoft.com/kb/139444[^]
http://www.sql-ex.ru/help/select17.php[^]
 
Share this answer
 

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