The following query is giving me differing results on different rows and it should be giving me the same result on all rows.
Here is the query:
select * from
(SELECT mjla_db.StudentRecordTable2.studentId,
mjla_db.StudentTable2.lastName as `Last Name`,
mjla_db.StudentTable2.firstName as `First Name`,
sum(if(quizId=60,quizGrade,0)) as `Quiz 60`,
sum(if(quizId=64,quizGrade,0)) as `Quiz 64`,
sum(if(quizId=71,quizGrade,0)) as `Quiz 71`,
(sum(quizGrade*(1-abs(sign(quizId-60)))) +
sum(quizGrade*(1-abs(sign(quizId-64)))) +
sum(quizGrade*(1-abs(sign(quizId-71)))) )/3
as Averages
FROM mjla_db.StudentRecordTable2, mjla_db.StudentTable2
where (mjla_db.StudentRecordTable2.studentId=mjla_db.StudentTable2.studentId)
GROUP BY studentId) as A
where A.studentId
in (select mjla_db.ClassStudentTable2.studentId
from mjla_db.ClassStudentTable2
where mjla_db.ClassStudentTable2.classId='CS3071F2011');
The number of quizzes are dynamic, number of students are dynamic.
Here is the contents of the table where mjla_db.StudentRecordTable2.classId='CS3071F2011'.
+-------------+-----------+-----------+--------+
| classId | studentId | quizGrade | quizId |
+-------------+-----------+-----------+--------+
| CS3071F2011 | A1 | NULL | 60 |
| CS3071F2011 | A2 | NULL | 60 |
| CS3071F2011 | A5 | NULL | 60 |
| CS3071F2011 | A1 | NULL | 64 |
| CS3071F2011 | A2 | NULL | 64 |
| CS3071F2011 | A5 | NULL | 64 |
| CS3071F2011 | A7 | NULL | 64 |
| CS3071F2011 | A3 | NULL | 64 |
| CS3071F2011 | A4 | NULL | 64 |
| CS3071F2011 | A3 | NULL | 60 |
| CS3071F2011 | A4 | NULL | 60 |
| CS3071F2011 | A7 | NULL | 60 |
| CS3071F2011 | A1 | NULL | 71 |
| CS3071F2011 | A2 | NULL | 71 |
| CS3071F2011 | A5 | NULL | 71 |
| CS3071F2011 | A7 | NULL | 71 |
| CS3071F2011 | A3 | NULL | 71 |
| CS3071F2011 | A4 | NULL | 71 |
+-------------+-----------+-----------+--------+
Here is the result of the query.
+-----------+-----------+------------+---------+---------+---------+----------+
| studentId | Last Name | First Name | Quiz 60 | Quiz 64 | Quiz 71 | Averages |
+-----------+-----------+------------+---------+---------+---------+----------+
| A1 | harry | thomas | 0 | 0 | 0 | 0.0000 |
| A2 | harry | willy | 0 | 0 | 0 | 0.0000 |
| A3 | billy | gregory | 0 | 0 | 0 | 0.0000 |
| A4 | goat | bobb | 0 | 0 | 0 | 0.0000 |
| A5 | nogood | tom | 0 | 0 | 0 | NULL |
| A7 | foobar | dick | 0 | 0 | 0 | NULL |
+-----------+-----------+------------+---------+---------+---------+----------+
The expected result of the query would be for all the averages to be 0.0000, why are the last two varying?
Also, whenever i do the query so that i only get one student at a time A5 and A7 still show up as NULL and the rest show up fine…
A solution to this problem or an alternate query to achieve the same goal would be acceptable. My goal is to get a table that each row is a representation of each student in the class and their grades on all the quizzes and an average of all the quizzes that student.
All of your quizGrade values are NULL, which you’re using in math calculations (
(sum(quizGrade*(1-abs(sign(quizId-60)))). As soon as a db NULL enters into pretty much any kind of operation, the whole operation becomes null. The question shouldn’t be why the last two are null, but why the first four AREN’T.