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
How to get all stored procedures in sql server?
Is sql server difficult to learn?
How to link tables in sql server?
Is natural join and equi join same?
How do I find the default sql server instance?
What do you mean by sql server 2005 express management tools?
How to apply cascading referential integrity in place of triggers?
Why SQL Agent is used?
Tell me the phases a transaction has to undergo?
What is the concept of optimization?
Explain rdbms?
What happens if you insert a duplicate key for the primary key column in ms sql server?
What are different types of table joins?
What are rest-style architecture's?
What is primary key, unique key, and foreign key?