SQL SERVER – Count Duplicate Records – Rows

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

Watch the view to see the above concept in action:

[youtube=http://www.youtube.com/watch?v=ioDJ0xVOHDY]

Reference : Pinal Dave (https://blog.sqlauthority.com) , SQL SERVER – Delete Duplicate Records – Rows

Duplicate Records, SQL Scripts
Previous Post
SQL SERVER – 2005 – List All Stored Procedure Modified in Last N Days
Next Post
SQL SERVER – 2008 – Scheduled Launch at an Event in Los Angeles on Feb. 27, 2008

Related Posts

187 Comments. Leave new

  • its a good solution so far …
    thanks

    Reply
  • Great solution, many thanks for this.

    Reply
  • 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

    Reply
  • Dear pinal,

    any luck with the solutions.

    Reply
  • 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

    Reply
  • Dude rana, why dont you find your own solution instead of scamming people who actually know how to write sql.

    Reply
  • 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.

    Reply
  • Great tip; exactly what I was looking for. Thanks for posting it.

    Reply
  • 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

    Reply
  • 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.

    Reply
  • It is great article. It real useful to me for quick result. Thank you, keep post this kind of articles.

    Reply
  • Thank you, your solution worth 5 marks for my coursework. ;-)

    Reply
  • THis works for me very nice!

    Thnx!

    Reply
  • 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.

    Reply
  • Thanks realy imagine

    Reply
  • Tri Aji Yudista A
    March 18, 2008 8:10 am

    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

    Reply
  • Hi Pinal,

    This is really too good solution to find and delete duplicate records from database table.

    Thanks ,
    Pravin

    Reply
  • 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.

    Reply
  • Thanks a lot.

    Reply
  • 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

    Reply

Leave a Reply