Ranking the results in mysql (mysql equivalents for 'dense_rank()' or 'row_number()' functions in oracle)

前端 未结 1 756
深忆病人
深忆病人 2021-01-21 18:28

Below is the sample data:

dept_id  salary
10       10000
10       20000
10       20000
10       30000
20       50000
20       60000

I want to g

相关标签:
1条回答
  • 2021-01-21 19:09

    Dunno if it's faster but seems like a lot less typing...

    SELECT x.*
         , IF(@prev = dept_id
                    , IF(salary <> @psalary,@i:=@i+1,@i:=@i)
             ,@i:=1) rank
         , @psalary := salary
         , @prev := dept_id prev 
      FROM my_table x
         , (SELECT @i:=1,@prev:='',@psalary:='') vars 
     ORDER 
        BY dept_id,salary;
    
    0 讨论(0)
提交回复
热议问题