我正在尝试编写一个查询,以获取表格中每个国家/地区的体育团队中的玩家数量:
我有一个包含以下字段的表
country | sports_team | player_name
我想要展示的是与此相似的结果
country | sports_team | number_of_players
即。我想遍历所有国家,并且对于sports_team的每一次变化,我都想记录团队中的球员数量。
样本表:
country | sports_team | player_name
c1 | teamA | name1
c1 | teamA | name2
c1 | teamB | name3
c2 | teamC | name4
c2 | teamC | name5
示例结果
country | sports_team | number_of_players
c1 | teamA | 2
c1 | teamB | 1
c2 | teamC | 2
我是编写SQL查询的新手,我不知道如何处理,任何帮助都将不胜感激!
答案 0 :(得分:3)
使用GROUP BY:
SELECT country, sports_team, COUNT(*)
FROM whatever_your_table_is
GROUP BY country, sports_team
答案 1 :(得分:3)
试试这个:
SELECT country, sports_team, COUNT(*) AS number_of_players
FROM yourtable
GROUP BY country, sports_team
ORDER BY country, sports_team;
像COUNT
这样的{p> Aggregate查询是此类数据处理的关键。
答案 2 :(得分:2)
select
country,
sports_team,
count(*) number_of_players
from table
group by country, sports_team
答案 3 :(得分:1)
试试这个:
SELECT
Country, Sports_Team, COUNT(player_name) AS number_of_players
FROM
YourTable
GROUP BY
Country, Sports_Team
答案 4 :(得分:1)
select country, sports_team, count(*) from <your table> group by country, sports_team
答案 5 :(得分:1)
无需循环,只需汇总。
SELECT country, sports_team, COUNT(*)
FROM myTable
GROUP BY country, sports_team
魔术在COUNT(*)
和GROUP BY
子句