l have a list of students score, i want to find the rank for each student according to his average
here is the table
steps to find the student rank.
create the view of average score for each student
CREATE VIEW mid_view as SELECT *,AVG(score) as score from midterm_result group by student_id
2.. find the rank
SELECT * , @rank := if( @last = average, @rank , @seq ) AS rank, @seq := @seq +1,
@last := average
FROM mid_view
ORDER BY average DESC
result is
my target is to reduce the steps, i want to reach at final result without creating the mid_view
How can i combine these to queries in single statement?
You could just include the first query as a sub query of the second one:
SELECT m.*, @rank := if(@last = average, @rank, @seq) AS rank,
@seq := @seq +1, @last := average
FROM
(
SELECT *, AVG(score) as score
from midterm_result
group by student_id
) m
ORDER BY average DESC
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.