In the following article we will go through a solution to a very common requirement of getting count of duplicate rows from SQL server table.
Let us first create a table and add sample data to this table. Col1 in the table is an identity column.
CREATE TABLE #Table1(col1 int, col2 int, col3 char(50))
INSERT INTO #Table1 values (1, 1, 'data value one')
INSERT INTO #Table1 values (2, 1, 'data value dup')
INSERT INTO #Table1 values (3, 2, 'data value two')
Now let us say we want to get the count of duplicate records based on values in column col2, the following query will get the desired result:
SELECT col2, COUNT(*) as Count
FROM #Table1
GROUP BY col2
HAVING COUNT(*) > 1
0 Comment(s)