Hi so i’m doing a small hobby project and i’m trying to get a GROUP BY clause to work correctly but it’s not grouping the results instead it just takes one column.
Example:
Table classes ( guID, className )
Table student ( guID, name, classID)
Table grades ( s_id, c_id, grade)
For example return the number of a certain grade for a whole class:
Students:
"Alex", "class1"
"Kevin", "class1"
"Lisa", "class2"
Grades:
"alex_id", "course_id", "B"
"alex_id", "course_id", "A"
"kevin_id", "course_id", "A"
"lisa_id", "course_id", "C"
QUERY:
SELECT classes.className, (SELECT COUNT(*) FROM grades WHERE s_id = student.guID AND grade = "A") as Total_Grades
FROM classes
INNER JOIN student
ON classes.guID = student.classID
WHERE classes.guID = 1
GROUP BY classes.className;
This only gives the result:
"class1", 1
Where it should give:
"class1", 2
Would appreciate some help if someone got some spare time.
I will show you how to get there in 2-steps
Assuming you only want to show the number of grades, the following query will do.
If we want to include the className, we need to add the group by clause since we want to add the className field to the output. Anything not in an aggregate function needs to go in the group by clause.