Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count all duplicates of each value

Tags:

sql

sql-server

I would like a SQL query for MS Jet 4.0 (MSSql?) to get a count of all the duplicates of each number in a database.

The fields are: id (autonum), number (text)

I have a database with a lot of numbers.

Each number should be returned in numerical order, without duplicates, with a count of all duplicates.

Number-fields containing 1, 2, 2, 3, 1, 4, 2 should return:

1, 2  
2, 3  
3, 1  
4, 1  
like image 574
Filip Haglund Avatar asked Sep 06 '25 12:09

Filip Haglund


2 Answers

SELECT   col,
         COUNT(dupe_col) AS dupe_cnt
FROM     TABLE
GROUP BY col
HAVING   COUNT(dupe_col) > 1
ORDER BY COUNT(dupe_col) DESC
like image 199
cetver Avatar answered Sep 08 '25 03:09

cetver


SELECT number, COUNT(*)
    FROM YourTable
    GROUP BY number
    ORDER BY number
like image 40
Joe Stefanelli Avatar answered Sep 08 '25 01:09

Joe Stefanelli