[1]:https://i.stack.imgur.com/R5Pzz.png我目前正在尝试列出每个assignmentID的最小值,最大值,平均值和计数。我用来创建图片中显示的代码如下:
SELECT assignment.assignmentID, assignment.assignmentName, grade.grade
FROM grade
JOIN assignment ON grade.assignmentID = assignment.assignmentID
当我将最小值,最大值,平均值,计数添加到SELECT函数时,它只返回赋值ID的结果1.如何让它为所有赋值执行此操作
答案 0 :(得分:0)
那是因为你没有按照作业记录进行分组。使用group by
在每个分配的基础上执行聚合:
select assignment.assignmentID, assignment.assignmentName,
count(*) as gradeCount, avg(grade.grade) as avgGrade,
min(grade.grade) as minGrade, max(grade.grade) as maxGrade
from assignment, grade
where assignment.assignmentID = grade.assignmentID
group by assignment.assignmentID, assignment.assignmentName
答案 1 :(得分:0)
我相信你想要:
SELECT a.assignmentID, a.assignmentName,
COUNT(*) as numgrades, MIN(g.grade), MAX(g.grade), AVG(g.grade)
FROM assignment a JOIN
grade g
ON g.assignmentID = a.assignmentID
GROUP BY a.assignmentID, a.assignmentName;
您的查询缺少GROUP BY
和聚合函数。这假定grade
是数字,因此AVG()
有意义。
请注意,我还引入了表别名,因此查询更容易编写和阅读。