无法使用 SQL 选择不同位置的平均分数
Posted
技术标签:
【中文标题】无法使用 SQL 选择不同位置的平均分数【英文标题】:Unable to select average scores for different locations using SQL 【发布时间】:2017-06-21 03:59:36 【问题描述】:我正在尝试选择特定位置的平均分数进行比较 目的。使用语句
SELECT l.location_name AS "Location Of Store",
AVG (f.score) AS "Average Score"
FROM feedback AS f
JOIN employee AS e
ON f.qr_id = e.qr_id
JOIN location AS l
ON e.location_id = l.location_id
WHERE e.company_id = '1'
results in
+ ----------------- + ------------- +
| Location of Store | Average Score |
+ ----------------- + ------------- +
| London | 5.3333 |
+ ----------------- + ------------- +
如果我降低平均水平
SELECT l.location_name AS "Location Of Store",
f.score AS "Average Score"
FROM feedback AS f
JOIN employee AS e
ON f.qr_id = e.qr_id
JOIN location AS l
ON e.location_id = l.location_id
WHERE e.company_id = '1'
Then the results I want to see however this isn't the average score
+ ----------------- + ------------- +
| Location of Store | Average Score |
+ ----------------- + ------------- +
| London | 9 |
| Bedfordshire | 2 |
| Buckinghamshire | 5 |
+ ----------------- + ------------- +
可能错过了一些简单的东西,我不能指望它!
【问题讨论】:
【参考方案1】:看起来您只是缺少“分组依据”子句。另外,我认为您的第一个语句甚至不会编译正确?
试试这个:
SELECT l.location_nameAS "Location Of Store", AVG (f.score) AS "Average Score"
FROM feedbackAS f
JOIN employeeAS e
ON f.qr_id= e.qr_id
JOIN locationAS l
ON e.location_id = l.location_id
WHERE e.company_id = '1'
GROUP BY l.location_name
Group by 子句实际上是按不同字段中的值对聚合函数的结果进行分组。聚合函数如 min、max、count、avg、stddev 等。
【讨论】:
真不敢相信我错过了,今天花了太长时间看这个屏幕。 top语句求所有结果的平均值,如图5.5所示。以上是关于无法使用 SQL 选择不同位置的平均分数的主要内容,如果未能解决你的问题,请参考以下文章
sql: 联系题26 ,27查询平均成绩大于等于 85 的所有学生的学号姓名和平均成绩,查询课程名称为「数学」,且分数低于 60 的学生姓名和分数