Solution with user defined variables and union all


  • 1
    B

    The logic is pretty simple, just OJ only accept one SQL statement, so put the init and unset into union all.

       select 1 Score, 1 Rank from dual where (@rank := 0) = 1 or (@pre := -1) = 1
            union all
       select t.Score, @rank Rank from 
            (select Score from Scores order by Score desc) t
          where (@rank := if(t.Score = @pre, @rank, @rank + 1)) > -1 and (@pre := t.Score) > -1
            union all
       select 1, 1 from dual where (@rank := null) is null and (@pre := null ) is not null;

Log in to reply
 

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