MYSQL 和 GROUP BY

2022-01-09 00:00:00 sql sum group-by mysql

我正在研究一个高中评分系统.

I'm working on a high school grading system.

在我的学校,可以通过修改问题来更改成绩,我将这些更改与日期一起存储.

At my school, grades can be changed by reworking problems and I store these changes with dates.

我有一个函数可以正确返回平均值,因为最近的成绩标有一个值为1"的当前"字段.我想让该函数能够返回关于过去日期的最新成绩.我正在绘制他们的平均值随时间变化的图表.

I have a function that properly returns averages because the most recent grade is flagged with a "current" field with a value of '1'. I'd like to make the function capable of returning the most recent grade with respect to a date in the past. I'm making a graph of how their average has changed over time.

我想做的是这样的:

select sum(grades.points) 
  from grades 
 where date < 'thedate' 
order by date DESC 
group by assignmentID

我不能使用 sum 和 group by.它错误...

I can't use sum and group by. It errors...

我能想到的最好的方法是进行子选择.还有其他想法吗?

The best I can think of is to do a sub-select. Any other thoughts?

推荐答案

GROUP BY 必须在 ORDER BY 之前:

GROUP BY has to come before ORDER BY:

  SELECT SUM(g.points) 
    FROM GRADES g
   WHERE g.date < 'thedate' 
GROUP BY g.assignmentid
ORDER BY g.date DESC 

相关文章