A
A
Alexey Lebedev2014-05-22 10:27:28
SQL
Alexey Lebedev, 2014-05-22 10:27:28

Top players. How to display the place of a specific player?

There is MS SQL and such a table, a fragment is shown:
id (unique key)
score (index by score DESC)
To get the top, it executes 2 queries:

SELECT TOP 100 id, score FROM table ORDER BY score DESC;
SELECT score FROM table WHERE [email protected];

But in the second query, you need to get not only the number of player points, but also his place.
How to do it in the most optimized way?
By the way? There are a lot of tops, according to different parameters. So the cluster index won't help here.
The table has a couple of hundred thousand records.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kfuntov, 2014-05-22
@swanrnd

SELECT score,
       (SELECT COUNT(*)
          FROM table AS p
         WHERE p.score <= t.score) AS position
 FROM table AS t WHERE [email protected];

Based on stackoverflow.com/questions/3614666/mysql-get-row-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question