开发者

MYSQL SUM GROUP BY

开发者 https://www.devze.com 2022-12-09 04:39 出处:网络
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.

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.

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.

What I'd like to do is something like this:

select sum(grades.points) 
  f开发者_开发知识库rom grades 
 where date < 'thedate' 
order by date DESC 
group by assignmentID

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 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 


Try this:

SELECT cat_name, SUM(amount) AS total_amount
FROM table
GROUP BY cat_name
0

精彩评论

暂无评论...
验证码 换一张
取 消