stefanosn
stefanosn

Reputation: 3324

Sql query how to get number of users group by city

Imagine the following table Users

Users
------------------------

CITY        USERNAME        TOPIC 
city1        george          topic1
city1        george          topic2
city1        george          topic3
city1        chris           topic1
city1        chris           topic2
city2        john            topic1
city3        jenny           topic1

I want the following result group by city

city1 -> 2 users
city2 -> 1 user
city3 -> 1 user

Any help appreciated!

Upvotes: 0

Views: 204

Answers (1)

Taryn
Taryn

Reputation: 247810

If you want the distinct users for each city:

select city, count(distinct username) TotalUsers
from users
group by city;

See SQL Fiddle with Demo. The result is:

|  CITY | TOTALUSERS |
----------------------
| city1 |          2 |
| city2 |          1 |
| city3 |          1 |

Upvotes: 3

Related Questions