For example I have table that has such columns as geography
and children
. I need to count how many there are childrens
from each country
.
I only could make such thing:
SELECT geography, COUNT( * )
FROM table1
GROUP BY children
ORDER BY geography
Well, I have for example:
Canada John
Canada John
Canada Peter
I need result such as from Canada there are 2 children
.
But If I will make group by children
there result is:
Canada 2
Canada 1
If group by geography
then,
Canada 3
SUM
is not really need here. The only thing you will do is to group it by geography
.
SELECT geography, COUNT(DISTINCT children) totalCount
FROM table1
GROUP BY geography
ORDER BY geography
If you need to count only the unique values in a group, you can use COUNT(DISTINCT expr)
SELECT geography, COUNT(DISTINCT children)
FROM table1
GROUP BY geography
ORDER BY geography
If geography is country field - you have to add it to group-by.
SELECT geography, COUNT(children)
FROM table1
GROUP BY geography
ORDER BY geography
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.