简体   繁体   中英

count multiple values from single column

I have a table with customer information like date of birth, address, contactinfo, etc.

I want to count the number of customers per city with a single query that outputs two values per record, cityname and amount of customers living there:

Alabama 285
Kentucky 167
New York 4
Rio de Janeiro 950
etc...

There are hundreds of cities in the table so I can't do a

SELECT count(CASE WHEN city = 'Alabama' THEN 1 END) AS Alabama

You can use GROUP BY clause to count the number of customers per city :

SELECT city
     , COUNT(*) 
FROM table 
GROUP BY city

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM