Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a table with some duplicate rows in it.
i want to delete only one duplicate row.
for example i have 9 duplicate rows so should delete only one row and should show 8 remaining rows.

example

                date         calling   called  duration    timestampp
2012-06-19 10:22:45.000	165	218	155	1.9         121
2012-06-19 10:22:45.000	165	218	155	1.9         121
2012-06-19 10:22:45.000	165	218	155	1.9         121
2012-06-19 10:22:45.000	165	218	155	1.9         121


from above date should delete only one row and should show 3 rows
2012-06-19 10:22:45.000	165	218	155	1.9         100
2012-06-19 10:22:45.000	165	218	155	1.9         100
2012-06-19 10:22:45.000	165	218	155	1.9         100

from above date should delete only one row and should show 2 rows

and etc..


Thanks and Regards
DSVS
Posted
Updated 20-Jun-12 5:49am
v2
Comments
luisnike19 20-Jun-12 10:55am    
Do you have an ID that identifies each record?
srinvas 20-Jun-12 11:02am    
we dont have ID each record is identical with other row

date calling called duration timestampp

2012-06-19 10:22:45.000 165 218 155 1.9 121
2012-06-19 10:22:45.000 165 218 155 1.9 121

Follow the advice in the following article:

http://www.4guysfromrolla.com/webtech/sqlguru/q051200-2.shtml[^]

However, instead of deleting every dulicate row, loop through each row. If it matches the previous row and your boolean flag is false, delete it and set a boolean flag to true. Then, if the next row matches, check the boolean flag and, since it is true, don't delete that row. When a row is different than the previous row, set the boolean flag to false. This will delete one of each duplicate row in your database.
 
Share this answer
 
Comments
Maciej Los 20-Jun-12 16:53pm    
Nice ;)
+5
SQL
delete t
from   yourtable t
where  yourColumn = (select top 1 yourColumn from yourtable order by . . . )
 
Share this answer
 
Comments
Maciej Los 20-Jun-12 16:57pm    
It should works, my 5!
Sameer Alomari 20-Jun-12 17:09pm    
;)

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