In my previous article SQL SERVER – Delete Duplicate Records – Rows, we have seen how we can delete all the duplicate records in one simple query. In this article we will see how to find count of all the duplicate records in the table. Following query demonstrates usage of GROUP BY, HAVING, ORDER BY in one query and returns the results with duplicate column and its count in descending order.
SELECT YourColumn, COUNT(*) TotalCount
FROM YourTable
GROUP BY YourColumn
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
Reference : Pinal Dave (http://blog.SQLAuthority.com) , SQL SERVER – Delete Duplicate Records – Rows




its a good solution so far …
thanks
Great solution, many thanks for this.
Dear Pinal,
I visited your blog so manytimes and get the solution. Thank you so much for keep updating your blog.
I have problem and i wanted you to give some time to resolve and given me a solution.
I have one table containing the records like this
Movie ID Movie Name New Name
1 Spider Man Spider Man
2 Spider Man 2 Spider Man
3 Spider Man 3 Spider Man
4 Spider Man UK Spider Man
5 Spider Man USA Spider Man
6 New Spider Man Spider Man
7 Spider Man Black Spider Man
8 Spider Man Part 1 Spider Man
9 Spider Man Part 2 Spider Man
10 Spider Man I Spider Man
11 Spider Man III Spider Man
12 Spider Man Part II Spider Man
My manufacturer send me the data in this format and i have to allocate there new name to do some comparison
I wanted to make this process automatic.
what i mean is i need a query which will give me a duplicate records and also the suggestion for the new name.
I am fully confident that you will help me out from this problem.
Looking forward
Rana
Dear pinal,
any luck with the solutions.
Rana,
Not yet, I many not have time to write custom solution in near future. I will look into it soon.
Regards,
Pinal
Please find some time to write custom solution.
I hope you understand my problem and how to resolve it.
Good Luck.
Looking forward on this weekend.
Rana
Dude rana, why dont you find your own solution instead of scamming people who actually know how to write sql.
@Dominator, @Rana,
Please calm down.
Rana, as I previously said, I have responsibility of running a large company and I have very little time.
I write SQL to help most of the people. Your request is very much specific and with restricted need and I do not see how it can be helpful to many readers.
I will be not able to comply your request. You will have to help yourself. Again, I am sorry, as you ordered I am not able to find sometime in weekend.
Good luck,
Regards,
Pinal Dave
Dear Pinal and Vistor Dominator,
I wrote here because i was wondering that you must have done something before so i can use that.
but no worries i have spent some time and get the solutions.
Few algo i wrote.
some counts and its really a wonderful solutions for even cleanign the data.
I will be posting this in this weekend.
I need to explain line by line otherwise people might not udnerstand.
thanks anyway.
Great tip; exactly what I was looking for. Thanks for posting it.
I want the show a result that choose the line that has the lowest price
What do you suggest
Table
Item Code Price
Spider $10.00
Spider $9.00
Ant $5.00
Ant $4.00
The result I want to see would be
Spider $9.00
And $4.00
Thanks
Thanks your solution was exactly what I was looking for. I had a bunch of ID’s with a different times of a code, that I need to know how many occurrences for each individual. Your code gave all that, and allow me to get the data to program accordingly.
It is great article. It real useful to me for quick result. Thank you, keep post this kind of articles.
[...] SQL SERVER – Count Duplicate Records – Rows [...]
Thank you, your solution worth 5 marks for my coursework. ;-)
THis works for me very nice!
Thnx!
Hi Pinal,
Thanks for the great info. I couldn’t believe how easy it is to accomplish what I was trying to do. I took what you wrote and modified it a bit since I only needed to know how many of each duplicate I had.
Thanks realy imagine
For 11. Alex, you could do this perhaps:
SELECT
ITEM_CODE,
MIN(PRICE) AS PRICE
FROM TABLE
GROUP BY ITEM_CODE
ORDER BY ITEM_CODE
Hi Pinal,
This is really too good solution to find and delete duplicate records from database table.
Thanks ,
Pravin
Hi Pinal,
One more soultion to Delete completely similar record from database table
We can play with row Id of the table row to delete a completely similar records , keeping one avilable using sysobjects in sql server 2005.
Thanks,
Pravin.
Thanks a lot.
select sales6.Sou_Enqu,(count(sales5.project_reg)),(count(sales5.project_EMRB))from sales6,sales5 where sales5.sales_no=sales6.sales_no and sales5.project_reg =’Regent Park’and sales5.project_EMRB=’ECB’ Group by sales6.Sou_Enqu;
this is my query i want count 10 field value in that query both field count value is displaed same.
source regent park ecb eck
newpeper 10 20 30
email 20 10 20
like that format
plz help me soon
tell me solution
Another solution is to put the table with duplicates in join with itself.
For a simple example see:
http://www.ugmfree.it/TipsTsql.aspx?tip=TipTsqlDeleteDuplicates
thx pinal…. it was very easy …
Pinal,
The query posted at the top of this page results with duplicate column and its count in descending order.
How can i find the count the returned rows???
Please help me.
Hi Pinal,
I have a question.
I need to loop through a table and if there are some duplicate values in one of the columns I need to add a suffix of a, b, c, and so on. But, there are different sets of duplicates. So, I need to do the suffix on each separate set of duplicates.
Please notice bellow, there are three sets of values that are duplicates. This is a sample of my data.
Could you give me any ideas on how to accomplish this task? Thank you.
ZP19188AMR
ZP19188AVE
ZP19188AVH
ZP19188AVH
ZP19188COC
ZP19188COC
ZP19188COC
ZP19188COC
ZP19188COC
ZP19188COC
ZP19188PHM
ZP19188PHM
ZP19188RIM
ZP19188SAB
ZP19188WEH
thanks for the post it really helped me.
Thanks. This is very helpful.
I need to limit the number of duplicates. Can this be achieved? Thanks.
Thanks.
HI Pinal,
If I want to select all the fields in a table,
How shold I write the query for that
thank u
thanks.
select Taluk_Name_Eng ,count(Taluk_Name_Eng)as kk from tbl_PL04_Taluk
what problem in this query
Using the above Query is nearly just what I need but it only gives me an ID (YourColumn). I have another table which would have a named value for this ID. How can I return that instead? A Join or a nested statement?
Many thanks
Did it, quite new to SQL I’m afriad, hope it helps another novice :)
SELECT top 20 computers.name as CID, COUNT(*) as ErrorCount
FROM errors
inner join computers
on computers.id = errors.computerid
GROUP BY computers.name
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
Hi,
I have written to get Duplicate Rows from the table. You can use the ROW_Number() in SQL 2005.
To read more about it:
http://tejasnshah.wordpress.com/2008/12/31/find-duplicate-rows-with-row_number-sql-server-2005/
Tejas
u r good
SELECT YourColumn, COUNT(*) TotalCount
FROM YourTable
GROUP BY YourColumn
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
this query is solved my lot of problems
Thank you very much
[...] SQL SERVER – Count Duplicate Records – Rows [...]
If you’d like to get a total count of the duplicates as I needed to do, try returning the above query as a derived table and then do a sum on the count.
SELECT SUM(Duplicates.TotalCount) FROM (SELECT YourColumn, COUNT(*) TotalCount
FROM YourTable
GROUP BY YourColumn
HAVING COUNT(*) > 1) AS Duplicates
My database has over 1200 duplicates. :(
Hey Pinal,
Excellent query ! thanks !
its really good update its help ful for every one thanks
i have a data entry system for patient records. in my system there are several forms through which data is entered in different tables. and there is a form which takes the present condition of the patient (i call it PresentAssessment). now i what i want is when the user enters a new record i want to compare the values of the attributes (database table fields) of the newly entered patient and from all the matching records i want to calculate and return the chance(in terms of %age) of particular disease(s) (stored in PresentAssessment) to newly added patient. e.g. if i have two tables i.e. PatientDrugHistory and PresentAssessment. and say each of them already have 10 records with 10 columns each. now when entering the 11th record in PatientDrugHistory i want to calculte and store how much chances (in terms of %age) exist that this 11th patient is suffereing from disease1 (a column in PresentAssessment). please if anybody can tell me what query to write for this purpose