Sunday, 22 April 2012

SQL: Query to find Duplicate Rows in a Table


If you would like to list duplicate rows with the count for a table, the following query would be helpful.
Query: Duplicate Rows

SELECT Column1,Column2,Column3,Count(*)
FROM dbo.TableName
GROUP BY Column1,Column2,Column3
HAVING ( COUNT(*) > 1 )

And there are scenarios where we need to find the combination of Columns in a table occurred only once. In such case the following query could be used.

Query: Unique combination of columns

SELECT Column1,Column2,Column3,Count(*)
FROM dbo.TableName
GROUP BY Column1,Column2,Column3
HAVING ( COUNT(*) = 1 )

Hope it helps.
Source : http://sapost.blogspot.in/

0 comments:

Post a Comment