MYSQL:根据平均值对结果进行排序

标签 mysql sql

我有一张这样的 table

------------------------------------------------------------
| id    | coursecodeid   | grade | workload  | easiness    |
------------------------------------------------------------
| 1     |  10            |  A+   |    5      |   4         |
| 2     |  10            |  A+   |    2      |   4         |
| 3     |  10            |  B    |    3      |   3         |
| 4     |  11            |  B+   |    2      |   3         |
| 5     |  11            |  A+   |    5      |   4         |
| 6     |  12            |  B    |    3      |   3         |
| 7     |  11            |  B+   |    7      |   8         |
| 8     |  11            |  A+   |    1      |   2         |
------------------------------------------------------------

我想根据每门类(class)计算的平均值对结果 (coursecodeid) 进行排序。

平均值是这样的

all workload values of the grade against courseid
+ all easiness values of the grade against courseid
/ 2 

在此示例中,coursecodeid = '10' 有三个条目

avgworkload(5+ 2 + 3)/3=x 
avgeasiness(4 + 4 + 3)/3 = y
answer x+y/2 = z

因此我们必须像这样计算每门类(class)的平均值,并根据我们得到的平均值显示coursecodeids

并且成绩应根据特定类(class)的最大出现次数来显示。

我正在运行此查询,但它没有按预期工作

SELECT 
 coursecode.id,
 feedback.grade CourseGrade,

 (( AVG( workload ) + AVG( easiness )) /2 ) as Avg
FROM coursecode
LEFT JOIN feedback ON feedback.coursecodeID = coursecode.id
GROUP BY coursecode.id
ORDER BY Avg DESC
LIMIT 11

最佳答案

试试这个

SELECT 
    (AVG(workload) + AVG(easiness)) / 2 AS result
FROM
    metagen.new_table
GROUP BY coursecodeid;

关于MYSQL:根据平均值对结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31935883/

相关文章:

sql - 在 Oracle 中的 JSON_VALUE() 中传递动态 key

sql - 如何在 PostgreSQL 中将行转换为列?

mysql - 为什么索引列在查询 `IS NULL` 时返回结果会很慢?

mysql - 当类具有 jdbc servlet jsp mysql 中另一个类的外键时,如何列出类的所有元素?

mysql - 在大型更新时从 mysql 触发器向 gearman 发送作业

SQL Server 2012如何查看数据?

mysql - 如何简化我的sql查询

php - 为 MYSQL 中的 foreach() 提供的参数无效,适用于第一个表而不是第二个表

mysql - MYSQL 中最常见名字的更快排名

mysql if 条件加值 else 加值1