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
How would you add a section to a table?
What is self contained multi valued query?
What are the disadvantages of primary key and foreign key in SQL?
How to get a list of columns using the "sys.columns" view in ms sql server?
What types of Joins are possible with Sql Server?
What does REVERT do in SQL Server 2005?
What are the default system databases in sql server 2000?
How do indexes help, types?
What protocol does sql server use?
How to see the event list of an existing trigger using sys.trigger_events?
How do users use Report Builder with SQL Server data sources?
explain different levels of normalization? : Sql server database administration
What are ddl triggers and types of ddl trigger?
How to create indexed view?
If a user does not have permission to a table, but has permission to a view created on it, will he be able to view the data in table?