How to retrieve duplicate rows in a table?
How to delete the duplicate entries in a table?
Answer Posted / skybeaver
/* selecting duplicate rows in a table */
select col1, col2, ..., colN, count(*)
from TableName
group by col1, col2, ..., colN
having count(*) > 1
/* deleting duplicate rows from a table */
select col1, col2, ..., colN, count(*) as "Duplicates"
into #duplicates
from TableName
group by col1, col2, ..., colN
having count(*) > 1
delete TableName
from TableName t, #duplicates d
where t.col1 = d.col1 and
....
t.colN = d.colN
/* damn I'm good! */
| Is This Answer Correct ? | 11 Yes | 5 No |
Post New Answer View All Answers
What are the different Topologies in which Replication can be configured?
List down some advantages of sql stored procedure?
What are the different types of data sources in ssrs?
What is difference between order by and group by?
How do you clear a log file?
What is index, cluster index and nonclustered index?
What are indexes in ms sql server?
What is a dbms wizard?
What is the sql case statement used for?
What to check if a User database is locked?
What's new in sql management studio for sql server? : sql server management studio
What is plan freezing?
How do I find the transaction log size in sql server?
where can you add custom error messages to sql server? : Sql server administration
How do indexes help, types?