简体   繁体   English

如何编写 SQL 查询平均值?

[英]How do I write SQL query for average?

I have recently attended an interview on SQL. Below is the question:最近参加了SQL的面试,问题如下:

Write a query to retrieve city name, state name, city population and return the following calculations along with the before mentioned fields.编写查询以检索城市名称、state 名称、城市人口并返回以下计算结果以及上述字段。

  1. Average city population in the state.平均城市人口在state。
  2. Difference between the city population and average city population in that state. state 的城市人口与平均城市人口之间的差异。

City Table:城市表:

City_Name城市名称 State_NAme State_NAme Population人口
Baltimore巴尔的摩 Maryland马里兰州 30000 30000
College Park大学公园 Maryland马里兰州 18000 18000
Columbia哥伦比亚 Maryland马里兰州 20000 20000
Boston波士顿 Massachusetts马萨诸塞州 35000 35000
Malden马尔登 Massachusetts马萨诸塞州 10000 10000
Dover多佛 Delaware特拉华州 20000 20000
Jersey City Jersey 城市 New Jersey新Jersey 35000 35000

I have tried below query but I didnot get desired output. Can anyone help me with correct query?我试过下面的查询,但没有得到所需的 output。任何人都可以帮助我进行正确的查询吗?

select * from city_table;

select state_name, sum(population)/count(city_name) as average_city_pop
from city_table
group by state_name;

You need to explore sql more, learn about aggregation functions and joins.您需要进一步探索 sql,了解聚合函数和连接。 Your query is correct in obtaining the average population.您的查询在获取平均人口方面是正确的。 You need to join it to the original table to get your result.您需要将它加入到原始表中以获得结果。

with avg_city_pop as (select state_name, avg(population) as avg from city group by state_name)
select c.*, acp.avg as average_city_population, abs(acp.avg-c.population) as difference  from city c inner join avg_city_pop acp on c.state_name = acp.state_name;

try it out here在这里试试

For MySQL versions before 8.0, we can use a correlated subquery to get the average population for each state, which can alss be used to calculate the gap between city population and state average population.对于8.0之前的MySQL版本,我们可以使用相关子查询得到每个state的平均人口,也可以用来计算城市人口与state平均人口的差距。 Note, the abs function is optionally used to always return a positive number.请注意,可选地使用abs function 始终返回正数。 Omit it if necessary.如有必要,请省略它。

select city_name,state_name,(select avg(population) from city where state_name=c.state_name group by state_name ) as average_pop ,
abs(population - (select avg(population) from city where state_name=c.state_name group by state_name )) as gap_pop 
from city c;
select 
   city_name,
   state_name,
   population,
   avg(population) over (partition by state_name) as Avg_pop,
   population-avg(population) over (partition by state_name) as Difference
from city;

see sqlfilldle: https://www.db-fiddle.com/f/tVxdH6bQvsu48umWgLmrQB/0参见 sqlfilldle: https://www.db-fiddle.com/f/tVxdH6bQvsu48umWgLmrQB/0

This needs MySQL 8.0+, because over_clause is available since 8.0.这需要 MySQL 8.0+,因为over_clause从 8.0 开始可用。

output: output:

city_name城市名称 state_name州名 population人口 Avg_pop平均流行 Difference区别
dover多佛 delaware特拉华州 20000 20000 20000.0000 20000.0000 0.0000 0.0000
baltimore巴尔的摩 maryland马里兰州 30000 30000 22666.6667 22666.6667 7333.3333 7333.3333
college park大学公园 maryland马里兰州 18000 18000 22666.6667 22666.6667 -4666.6667 -4666.6667
columbia哥伦比亚 maryland马里兰州 20000 20000 22666.6667 22666.6667 -2666.6667 -2666.6667
boston波士顿 massachussets马萨诸塞州 35000 35000 22500.0000 22500.0000 12500.0000 12500.0000
molden模具 massachussets马萨诸塞州 10000 10000 22500.0000 22500.0000 -12500.0000 -12500.0000
jersey city jersey 城市 new jersey新 jersey 35000 35000 35000.0000 35000.0000 0.0000 0.0000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM