Write a query to delete duplicate records in SQL SERVER

Answer Posted / tallapaneni

create table emp(eid int, ename varchar(50),age int)

insert into emp values( 1,'ramu1,21)
insert into emp values( 1,'ramu2,22)
insert into emp values( 2,'ramu3,22)
insert into emp values( 3,'ramu4,24)
insert into emp values( 4,'ramu5,25)
insert into emp values( 5,'ramu6,26)
insert into emp values( 2,'ramu7,27)
insert into emp values( 6,'ramu8,28)
insert into emp values( 6,'ramu9,29)

SELECT * FROM emp

WITH aaa AS (SELECT (ROW_NUMBER() OVER (PARTITION BY eid
ORDER BY eid)) AS RNum FROM emp)
DELETE FROM aaa WHERE RNum IN (SELECT a.RNum FROM aaa AS a,
aaa AS b WHERE a.RNum > b.RNum GROUP BY a.RNum)

SELECT * FROM emp order by eid



Regards,
Raaam

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the recovery models for a database?

619


What is the stuff and how does it differ from the replace function?

539


What is the cartesian product of the table?

554


Can you always create a cache of a report?

115


How to encrypt data between dialogs?

574






application server is slow what may be the problem

1784


What are the basic features of a trigger in ms sql server?

569


What is the maximum size of a dimension? : sql server analysis services, ssas

564


How can you ensure that the database and sql server based application perform well?

638


What is tablix?

115


Is the order of columns in the set clause important in ms sql server?

521


What is a result set object returned by mssql_query()?

580


Describe in brief system database.

548


Name few of the dcl commands in sql?

555


Is there any difference between primary key and unique with the not null condition?

530