The following query is useful to delete duplicate rows. The table in this example has
ID as an identity column and the columns which have duplicate data are Column1, Column2 and Column3.DELETE FROM TableName
WHERE ID NOT IN (select * from(SELECT MAX(ID)
FROM TableName
GROUP BY Column1,
Column2,
Column3
/*Even if ID is not null-able SQL Server treats MAX(ID) as potentially
nullable. Because of semantics of NOT IN (NULL) including the clause
below can simplify the plan*/
HAVING MAX(ID) IS NOT NULL)) as t
The following script shows usage of
GROUP BY, HAVING, ORDER BY in one query, and returns the results with duplicate column and its count.SELECT YourColumnName,
COUNT(*) TotalCount
FROM YourTableName
GROUP BY YourColumnName
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC

0 comments:
Post a Comment