mysql-- score rankings

                                                   

Problem-solving ideas:

  1, first of all, we need to scores reverse order (hereinafter the result is not too heavy to reverse the score)

    select Score from Scores order by Score DESC

  2, then we still lack a Rank. For example: a class of 20 people, but everyone took the test 95 minutes, then the whole class rank are tied for first or only one; if some students took the test 95, a portion of the exam, 90, a portion of the exam, 85, then the whole performance rankings to be divided into three ranking. (The following is the result of the total ranking after de-emphasis)

    select count(distinct Score) from Scores

  3, and finally considering how to combine the previous two, and their ranking will be displayed. When we execute the second step, so to consider whether, in the basis of the scores go heavy on the total number of statistics, we add a condition that the total number of statistics to re-screened while we score higher than our human one number ( because it is the de-emphasis of this statistical error, the final ranking is out of the person's true ranking, does not appear such as two people are tied for first, and the next man is ranked third case ) is here understood +1 is used as part of the conditions> instead of> =, if using> = +1, then there is no need of, insight Act II

(A solution) Select 

       a.Score as score, (select count(distinct b.Score)+1 from Scores as b where b.Score > a.Score) as Rank
        from Scores as a order by a.Score DESC

(Solution two) Select 

       a.Score as score, (select count(distinct b.Score) from Scores as b where b.Score >= a.Score) as Rank
       from Scores as a order by a.Score DESC
 
 
Figure execution results

 

 

Guess you like

Origin www.cnblogs.com/vegetableDD/p/11619323.html