1

I have a table with the following data

USER_ID | REVENUE
1  | 12,545
2  | 9,845
3  | 55,874

Is there a way for me to create a result with ranking on the fly within a view i.e.

USER_ID | REVENUE | RANK
3  |   55,874  |  1
1  |   12,545  |  2
2  |   9,845   |  3

I could try doing this by running a cron, but would like the result to be real-time - generated by MySQL in a view.

Any other alternatives welcome.

buzzmonkey
  • 13
  • 2

2 Answers2

0

It's a hack, but as two separate queries:

select @last:=0;

select USER_ID, REVENUE, @RANK := @RANK + 1 as RANK
from YOURTABLE
order by ...
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

Yes, it can be done in one query

select user_id, 
       revenue, 
       @rank := rank + 1 as rank
from your_table, (select @rank := 0) r
order by revenue desc
juergen d
  • 201,996
  • 37
  • 293
  • 362