MYSQL 和 GROUP BY

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)