简体   繁体   中英

MYSQL SELECT CASE with SUM and MAX

In this project I'm trying to create a query that returns the total of GPA points of a college student according to his/her grades. I'm using MySQL 5.7.19 and the data is stored as follows:

CourseID     Grade
5              A
8              D-
4              B-
8              B+
2              A

I used a CASE statement to convert the letter grades to numbers and used the MAX function combined with GROUP BY to get only the higher grade from courses taken more than once (in this case course # 8)...

SELECT MAX(
    CASE WHEN Grade = 'A+' OR Grade = 'A' THEN 4
        WHEN Grade = 'A-' THEN 3.67
        WHEN Grade = 'B+' THEN 3.33
        WHEN Grade = 'B'  THEN 3
        WHEN Grade = 'B-' THEN 2.67
        WHEN Grade = 'C+' THEN 2.33
        WHEN Grade = 'C'  THEN 2
        WHEN Grade = 'C-' THEN 1.67
        WHEN Grade = 'D+' THEN 1.33
        WHEN Grade = 'D'  THEN 1
        WHEN Grade = 'D-' THEN 0.67
        ELSE 0 END) AS Grade
FROM courses_taken WHERE Grade IS NOT NULL AND Grade != 'W'
GROUP BY CourseID

That works fine, I get the following results:

Grade
4.00
2.67
3.33
4.00

But I need to get the sum of those values and when I try using the SUM function I get an error:

SELECT SUM(MAX(
    CASE WHEN Grade = 'A+' OR Grade = 'A' THEN 4
        WHEN Grade = 'A-' THEN 3.67
        WHEN Grade = 'B+' THEN 3.33
        WHEN Grade = 'B'  THEN 3
        WHEN Grade = 'B-' THEN 2.67
        WHEN Grade = 'C+' THEN 2.33
        WHEN Grade = 'C'  THEN 2
        WHEN Grade = 'C-' THEN 1.67
        WHEN Grade = 'D+' THEN 1.33
        WHEN Grade = 'D'  THEN 1
        WHEN Grade = 'D-' THEN 0.67
        ELSE 0 END)) AS Total
FROM courses_taken WHERE Grade IS NOT NULL AND Grade != 'W'
GROUP BY CourseID

ERROR 1111 (HY000): Invalid use of group function

I tried removing the GROUP BY clause but I got the same error. I also removed MAX and GROUP BY, it worked, however, I got the total of all the grades, which is not what I want. I was wondering what exactly I'm doing wrong or if there is any other way to accomplish this.

Thanks.

You can't stack multiple aggregate expressions in the same query. The trick is to use a sub query for the inner aggregate & wrap it in another query for the 2nd aggregate.

SELECT SUM(Grade) Total
FROM (
SELECT MAX(
    CASE WHEN Grade = 'A+' OR Grade = 'A' THEN 4
        WHEN Grade = 'A-' THEN 3.67
        WHEN Grade = 'B+' THEN 3.33
        WHEN Grade = 'B'  THEN 3
        WHEN Grade = 'B-' THEN 2.67
        WHEN Grade = 'C+' THEN 2.33
        WHEN Grade = 'C'  THEN 2
        WHEN Grade = 'C-' THEN 1.67
        WHEN Grade = 'D+' THEN 1.33
        WHEN Grade = 'D'  THEN 1
        WHEN Grade = 'D-' THEN 0.67
        ELSE 0 END) AS Grade
FROM courses_taken WHERE Grade IS NOT NULL AND Grade != 'W'
GROUP BY CourseID) max_grade

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM