MYSQL 和 GROUP BY [英] MYSQL SUM GROUP BY

查看:32
本文介绍了MYSQL 和 GROUP BY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 

这篇关于MYSQL 和 GROUP BY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆