adspace


Delete duplicate rows from a table without primary key by
using a single query
Table Employee
empname salary
A 200
B 300
A 200
C 400
D 500
D 500

Output should be

A 200
B 300
C 400
D 500

Answer Posted / swati tripathi

declare myCursor cursor for
select empid
from employee
group by empid
having count(*) > 1

declare @EmpId int


OPEN myCursor;
FETCH NEXT FROM myCursor INTO @EmpId
WHILE @@FETCH_STATUS = 0
BEGIN

delete top(select count(*)-1 from employee where
empid=@EmpId) from employee where empid=@EmpId

FETCH NEXT FROM myCursor INTO @EmpId
END
close myCursor

deallocate myCursor

Is This Answer Correct ?    11 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

If you're given a raw data table, how would perform etl (extract, transform, load) with sql to obtain the data in a desired format?

1047


Explain “row_number()” in sql server with an example?

1079


Disadvantages of the indexes?

1223


How to remove duplicate rows from table except one?

1088


If any stored procedure is encrypted, then can we see its definition in activity monitor?

1071


Why we use the openxml clause?

1079


What is a coalesce function?

1283


How to rebuild the master database?

1166


Can one drop a column from a table?

1089


When should you use an instead of trigger?

1052


How do I find the sql server version?

1102


What is clustered index

1086


You have a stored procedure, which execute a lengthy batch job. This stored procedure is called from a trigger you do not want to slow the data entry process you do not want trigger to wait for this batch job to finish before it completes itself what you can do to speed up the process?

1213


Why and when do stored procedure recompile?

1060


How to provide default values to function parameters?

1258