August 29, 2012

Display duplicate records

I don't know where I found this or if I made it up... I've used it for years.  I use this script to find out how many duplicate entries exist for specific columns.  It's especially handy when an application automatically assigns UID's:
 
SELECT  [COLUMN1]
      , [COLUMN2]
      , COUNT([COLUMN1]) AS [COLUMN1]_Count
      , COUNT([COLUMN2]) AS [COLUMN2]_Count
FROM    [TABLE]
GROUP BY [COLUMN1]
       , [COLUMN2]
HAVING  ( COUNT([COLUMN1]) > 1 ) AND
        ( COUNT([COLUMN2) > 1 )