user1218172
user1218172

Reputation: 193

Need sum of distinct values in one MySQL column grouped by the distinct value

I have a MySQL column named "country" in table named "contact".

I need to write a query to return all the distinct countries in that column, and how many times they occur.

I am trying with:

SELECT SUM([DISTINCT] country) FROM contact GROUP BY country

Where am I going wrong? Thanks!

Upvotes: 0

Views: 39

Answers (2)

user3005791
user3005791

Reputation: 106

Following should work

SELECT COUNT(country) AS Num_of_country, country
FROM Contact 
GROUP BY country

Upvotes: 1

Hunter McMillen
Hunter McMillen

Reputation: 61512

This will return the name of the Country and the number of times that Country appeared in the column:

SELECT country, COUNT(*) as count 
FROM Contact
GROUP BY country
ORDER BY count DESC

it also returns the countries in order of most to least frequent.

Upvotes: 3

Related Questions