0

例如,我有一个表,其中包含 和 等geographychildrenchildrens我需要计算每个有多少country

我只能做这样的事情:

SELECT geography, COUNT( * ) 
FROM table1
GROUP BY children
ORDER BY geography

好吧,我有例如:

Canada John
Canada John
Canada Peter

我需要结果,例如来自 Canada 有 2 children
但如果我会这样做group by children,结果是:

Canada 2
Canada 1

如果group by geography那时,

Canada 3
4

3 回答 3

4

SUM这里不是真的需要。您唯一要做的就是将其分组geography

SELECT geography, COUNT(DISTINCT children) totalCount
FROM table1
GROUP BY geography
ORDER BY geography
于 2012-10-30T04:38:34.870 回答
1

如果您只需要计算组中的唯一值,您可以使用COUNT(DISTINCT expr)

SELECT geography, COUNT(DISTINCT children) 
FROM table1
GROUP BY geography
ORDER BY geography
于 2012-10-30T04:37:44.210 回答
0

如果地理是国家/地区字段 - 您必须将其添加到分组依据。

SELECT geography, COUNT(children)
FROM table1
GROUP BY geography
ORDER BY geography
于 2012-10-30T04:40:39.663 回答