tags:

views:

168

answers:

1

I'm trying to do something similar to this, in Django. This is part of the page of Anna:

Pos NickName      Points
--- ---------     ------
1   The best      1000
...
35  Roger         550
36  Anna          545
37  Paul          540

It's a chart showing the scoring system, and it intends to show the first position, as well as the relative position of the presented player.

Showing the first one it's easy, as it's only making a query to the database and extracting the first one:

 best = Score.objects.all().order_by('-points')[0]

But I'm having problems getting the ones close to the presented player (Anna, in this case). I don't want to go searching through the complete list, as the complete list of players can be quite long.

Maybe there's a way to know the position a register occupies in an ordered list...

Any ideas on how to achieve it?

+2  A: 

Get the COUNT() of records that have a higher points.

Ignacio Vazquez-Abrams
Great solution, thanks!
Khelben
By the way, it has a curious problem. If there are two positions with the same number of points, the result will be different each time (at least, it can be, and in the practice, it does, using Django over MySQL). At the end, I had to make a complete sorting (first by points, then by name, for example) to keep consistency and avoid strange movements on rankings...
Khelben