I have a result portal where results are entered based on individual subject per term. I need to get the class highest score, average score, and minimum score per subject from the sum of three terms ( 1 academic year)
My code gets each of the subjects per term for each student but I need to get the highest, average, and lowest score after adding the three terms for each subject
My Query
SELECT results.subjects,
MAX(CASE WHEN results.term = 'First' THEN results.examTotal END) 'First'
,MAX(CASE WHEN results.term = 'Second' THEN results.examTotal END) 'Second'
,MAX(CASE WHEN results.term = 'Third' THEN results.examTotal END) 'Third'
FROM results
WHERE studId = '".$RegNo."'
GROUP BY results.subjects
ORDER BY results.subjects ASC
I expect to have a table
+-----+-------+--------+-----+----------+---------+---------+-------+
+Sub + First + Second +Third+(Total/3) + Highest + Average + Lowest+
+-----+-------+--------+-----+----------+---------+---------+-------+
+ Eng + 79 + 80 + 67 + 75.33 + 80 + 60 + 45 +
+-----+-------+--------+-----+----------+---------+---------+-------+
+Maths+ 60 + 77 + 73 + 70 + 90 + 60 + 40 +
+-----+-------+--------+-----+----------+---------+---------+-------+
You can get all the columns you need with your query, except (Total/3)
for which is better to use an outer query to calculate it:
SELECT
t.subjects, t.First, t.Second, t.Third,
round((t.First + t.Second + t.Third) / 3.0, 1) `(Total/3)`,
t.Highest, t.Average, t.Lowest
FROM (
SELECT
subjects,
MAX(CASE WHEN term = 'First' THEN examTotal END) First,
MAX(CASE WHEN term = 'Second' THEN examTotal END) Second,
MAX(CASE WHEN term = 'Third' THEN examTotal END) Third,
MAX(examTotal) Highest,
MIN(examTotal) Lowest,
AVG(examTotal) Average
FROM results
WHERE studId = '".$RegNo."'
GROUP BY subjects
) t
ORDER BY t.subjects ASC
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.