所以我有这张桌子
`gender country age name ID
male VietNam 20 Q 890
female China 30 K 209
female Japan 25 O 302
female VietNam 20 L 203
male China 20 E 504`
我想提出一个查询,可以列出具有相同性别,同一国家和相同年龄的人数,按计数和分组来计算
Select gender,COUNT(*) from Employee Group BY gender
Select country,COUNT(*) from Employee Group BY country
Select age,COUNT(*) from Employee Group BY age
有没有办法只用一个选择? 提前谢谢,抱歉我的英文不好
答案 0 :(得分:2)
你可以通过UNION将这些结果放在结果集的单独记录中:
Select 'Gender' as GroupByType, gender as GroupByValue ,COUNT(*) from Employee Group BY gender
UNION ALL
Select 'Country', country,COUNT(*) from Employee Group BY country
UNION ALL
Select 'Age', age,COUNT(*) from Employee Group BY age
答案 1 :(得分:2)
如果你的RDBMS支持它...... SQL Server,Oracle和其他人做....不确定你在用什么。
Group by Grouping sets
Select count(*), gender, country, age
from employee
group by grouping sets ((gender), (country), (age ))
并且根据您要查找的输出类型...这将导致4列计数,性别,国家/地区和年龄列,其中4列中的2列始终为空。
除了没有groupbyType之外,它会产生类似于JNevill的输出,而不是group by