I have two tables:
TABLE LIST:
student_id class_id subject_1 subject_2 subject_3 subject_4 subject_5
----------- ----------- ----------- ----------- ----------- ----------- ----------
1 9 78 87 95
2 9 67 95 87
3 9 85 84 85
4 10 70 65 78
5 10 75 80 81
6 10 80 75 82
Table subject_names
column_name subject_name
--------------- -------------
subject_1 English
subject_2 Chemistry
subject_3 Economics
subject_4 Accounts
subject_5 Biology
Now I need to create such a report for class_id = 9
column_name subject_name no_of_students
--------------- ------------- --------------
subject_1 English 3
subject_2 Chemistry 3
subject_3 Economics 0
subject_4 Accounts 0
subject_5 Biology 3
In short, I have to generate a report with the column names, subject_name and the number of students from class_id = 9 (or 10, independently) that appeared for this object.
All I managed to do is
1.
SELECT sn.column_name, sn.subject_name FROM subject_names sn;
and
2.
SELECT ml.class_id,
count(ml.subject_1) AS s1,
count(ml.subject_2) AS s1,
count(ml.subject_3) AS s1,
count(ml.subject_4) AS s1,
count(ml.subject_5) AS s1,
FROM marklist ml
WHERE ml.class_id = 9;
I do not understand how I can go forward and rotate the results of query 2 using the query. I can go in the wrong direction, but I have no idea.
source
share