SELECT student_id, section, count( * ) as total
FROM raw_data r
WHERE response = 1
GROUP BY student_id, section
The test has 4 sections, each of which has a different number of questions. I want to know, for each student and each section, how many questions they answered correctly (answer = 1).
However, with this query, if the student has no questions in this section, this line will be completely absent in my result set. How can I make sure that 4 rows are ALWAYS returned for each student, even if "total" for row 0?
Here my result is as follows:
student_id section total
1 DAP
1 MEA
1 NNR
2 DAP
2 MEA
2 NNR
3 DAP
3 MEA
3 NNR
4 DAP
4 DAP
4 MEA
4 NNR
Thank you for understanding!
UPDATE: I tried
SELECT student_id, section, if(count( * ) is null, 0, count( * )) as total
and that did not change the results at all. Other ideas?
UPDATE 2: I got the job thanks to the answer below:
SELECT student_id, section, SUM(CASE WHEN response = '1' THEN 1 ELSE 0 END ) AS total
FROM raw_data r
WHERE response = 1
GROUP BY student_id, section
Jen