Quantcast
Viewing latest article 1
Browse Latest Browse All 3

Answer by DBeck for Rank rows using weighted average with multiple sub queries

Here is the solution to the problem:

SELECT * FROM 

(

SELECT student_id, avg, exam,(exam) + (avg) as total, (@rn := @rn + 1) AS rank FROM (SELECT student_id,     (        SELECT AVG(g.grade)  / 100 * 40         FROM grades AS g        WHERE g.type != 'Exam' AND  g.student_id = grades.student_id    ) as avg,    (        SELECT AVG(s.grade)/100 * 60         FROM grades AS s        WHERE s.type = 'Exam' AND  s.student_id = grades.student_id    ) as exam FROM grades WHERE class_id = 2 AND subject_id = 1 GROUP BY student_id ) aggCROSS JOIN (SELECT @rn := 0) CONST ORDER BY total desc

) t


Viewing latest article 1
Browse Latest Browse All 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>