MYSQL SUM GROUP BY(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MYSQL 和 GROUP BY


基础教程推荐
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01