SELECT student_id, section, count(*) as total
FROM raw_data r
WHERE response = 1
GROUP BY student_id, section
それぞれ4つのセクションがあり、それぞれ異なる数の質問があります。私は、各学生と各セクションについて、彼らが正しく答えた質問(回答= 1)を知りたい。mysql SELECT COUNT(*)... GROUP BY ...カウントがゼロの行を返さない
ただし、このクエリでは、特定のセクションで質問がない場合、その行は結果セットから完全になくなります。行の「合計」が0であっても、すべての生徒に対して4行が常に返されるようにするにはどうすればよいですか?
は、ここに私の結果セットは次のようになります。任意の洞察力のための
student_id section total
1 DAP--29 3
1 MEA--16 2
1 NNR--13 1 --> missing the 4th section for student #1
2 DAP--29 1
2 MEA--16 4
2 NNR--13 2 --> missing the 4th section for student #2
3 DAP--29 2
3 MEA--16 3
3 NNR--13 3 --> missing the 4th section for student #3
4 DAP--29 5
4 DAP--30 1
4 MEA--16 1
4 NNR--13 2 --> here, all 4 sections show up because student 4 got at least one question right in each section
ありがとう!
UPDATE:私は
SELECT student_id, section, if(count(*) is null, 0, count(*)) as total
を試してみましたが、それはすべての結果を変更しませんでした。他のアイデア?
UPDATE 2:何WHERE
条件はありませんことを
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
入力がどのように表示されるかを示す必要があります。 –