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
Explain how dts is used to extract, transform and consolidate data?
What will happen if a column containing char type data is changed to the nchar data type?
What is nested transaction?
What is difference between materialized view and view?
How to test odbc dsn connection settings?
Can a table be created inside a trigger?
What is the minimum and maximum number of partitions required for a measure group? : sql server analysis services, ssas
What are some of the pros and cons of not dropping the sql server builtinadministrators group? : sql server security
Explain Capacity planning to create a database? and how to monitor it?
What are the different types of upgrades that can be performed in sql server?
What are the recovery models in sql server 2000?
List the data types available in mssql?
What is primary key and example?
What you can do to delete a table without the delete trigger firing?
sql database suspect We have a sql database that is showing as suspect. How can we recover?