Accepted solution with variables


  • 3
    B
    select Score,
      case 
        when @prevScore = Score then @rank
        when (@prevScore := Score) is not null then @rank := @rank+1
      end as Rank
    from Scores, (select @rank := 0, @prevScore := NULL) a
    order by Score desc;

  • 0
    T

    share an useful link:
    http://fellowtuts.com/mysql/query-to-obtain-rank-function-in-mysql/

    Return string not int :(


  • 0
    B

    I had a very very similar idea as yours, only differs in the line
    (@prevScore := Score) is not null

    Could someone kindly explain to me what's happening in this line?
    I know that if is not null is not here, we would have trouble finding the correct answer then the Score equal to zero and there's only one row. But why adding is not null solve this problem?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.