【问题标题】:I want help in a query: What is the average population of the districts in each country?我在查询中需要帮助:每个国家/地区的地区平均人口是多少?
【发布时间】:2022-01-23 21:54:57
【问题描述】:

我只有一张城市表:

ID Name Country Code District Population
6 Rotterdam NLD Zuid-Holland 593321
3878 Scottsdale USA Arizona 202705
3965 Corona USA California 124966
3973 Concord USA California 121780
3977 Cedar Rapids USA Iowa 120758
3982 Coral Springs USA Florida 117549
1613 Neyagawa JPN Osaka 257315
1630 Ageo JPN Saitama 209442

预期结果是:

countrycode avg(population)
JPN xxxxxx
NLD xxxxxxx
USA xxxxxxx

我使用了共享代码,但没有得到预期的答案:

select avg(population)
from city
where countrycode='JPN' and 'USA' and 'NLD'
group by district; 

上面的代码给了我一个空白结果“avg(population)” - 空白。

我正在使用 SQL 工作台

【问题讨论】:

  • MySQL Workbench,顾名思义,是 MySQL 的 IDE,而不是 SQL Server。您真正在使用哪个?
  • 尝试将“countrycode”也放入 SELECT 语句中。
  • select countrycode, avg(population) from city group by countrycode;

标签: sql


【解决方案1】:

试试:

select countrycode, avg(population) as avg_population
from city
where countrycode in('JPN', 'USA', 'NLD')
group by countrycode;

【讨论】:

  • 谢谢。此代码有效。
【解决方案2】:

这应该可以完成工作:

select countrycode, avg(population)
from city
where countrycode in('JPN','USA','NLD')
group by countrycode;

【讨论】:

  • 这段代码给了我一个错误 1064
猜你喜欢
  • 2015-04-06
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2019-06-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多