Friday, 6 July 2012

How to count duplicate records in sql table

Here am showing how to count no. of rows have same column values in a table,

SELECT YourColumnCOUNT(*) TotalCountFROM YourTableGROUP BY YourColumnHAVING COUNT(*) > 1ORDER BY COUNT(*) DESC




example:



SELECT EntityName, COUNT(*) TotalCount
FROM tblTest
GROUP BY EntityName
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC


if you want to know row value count in a table, like


EntityName
a
b
a
a
b
b
c
d




i want to get result like


EntityName | TotalCount
a                2
b                3
c                1
d                1


then use query like



SELECT EntityName, COUNT(*) TotalCount
FROM tblTest
GROUP BY EntityName
ORDER BY COUNT(*) DESC

No comments:

Post a Comment