Below is the sample data:
dept_id salary 10 10000 10 20000 10 20000 10 30000 20 50000 20 60000
I want to g
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;