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
What is right outer join in sql server joins?
Explain what is public role in sql server?
How to create function without parameter in sql server?
How to execute multiple stored procedures at one time in sql server?
What is the use of group by clause?
Create a dts package to produce a text file using the ‘update statistics’ command for the tables in a database with obsolete statistics.
what does the automatic recovery do? : Sql server administration
Which operator do you use to return all of the rows from one query except rows are returned in a second query?
How to sort the query output with order by clauses in ms sql server?
Explain the phases a transaction has to undergo?
What is conditional split?
What are the risks of storing a hibernate-managed object in a cache? How do you overcome the problems?
What do you understand by user-defined function in the sql server?
What is the difference between ddl,dml and dcl commands?
How to use go command in "sqlcmd"?