簡體   English   中英

如何使用具有不同值的COUNT(*)函數?

[英]How to use COUNT(*) function with distinct values?

例如,我的表具有諸如geographychildren類的列。 我需要計算每個country有多少個childrens

我只能做這樣的事情:

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

好吧,我有例如:

Canada John
Canada John
Canada Peter

我需要從加拿大來的結果,例如有2個children
但是,如果我要按group by children結果是:

Canada 2
Canada 1

如果group by geography

Canada 3

SUM在這里並不需要。 您唯一要做的就是按geography將其分組。

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

如果只需要計算組中的唯一值,則可以使用COUNT(DISTINCT expr)

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

如果地理位置是國家/地區字段-您必須將其添加到分組依據。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM