Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
consider the following scenario,

Table1:no primary key
Col1 col2 col3
a 1 0
b 2 0
c 1 0

now i want to delete row where two rows having same col2 value

Output:
Col1 col2 col3
a 1 0
b 2 0


Can any one help me to solve this problem.

Thanks.
Posted

Try:
SQL
WITH myCur AS 
   (SELECT ROW_NUMBER() OVER (PARTITION BY Col2 ORDER BY Col1) RowNo
    FROM   MyTable)
DELETE FROM myCur
WHERE RowNo > 1


[edit]Forgot the ORDER BY :doh: - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Karthik Achari 1-Jul-14 2:19am    
Thanks a lot for spending your valuable time.

consider for bulk data,i want to delete rows having same col2 value
OriginalGriff 1-Jul-14 3:40am    
Try it: it does exactly that! :laugh:

(Well, it does now I've added the ORDER BY clause :blush: )
Hi,

Try this.

SQL
DELETE FROM table1 WHERE col1 NOT IN (SELECT MIN(col1) FROM table1  group by col2 ))



Cheers.
 
Share this answer
 
Comments
Karthik Achari 1-Jul-14 2:18am    
consider for bulk data,i want to delete rows having same col2 value
Magic Wonder 1-Jul-14 3:40am    
Have you tried the same with your data?

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