Search code examples
mysqlcountranking

MySQL : How to count the position of the results in a Select?


I need to count the position of the results in a Select - like a RANKING.

I have this:

"SELECT PLAYERID, SUM(POINTS) AS POINTS FROM TABLE1 GROUP BY PLAYERID ORDER BY POINTS"

And I Get this:

PLAYERID   POINTS
4          200
2          150
1          100
3           80

I need to know the position in the Ranking for PlayerID = 1.

I'm not an expert in MySQL - but it's possible to count the results like this:

RK   PLAYERID   POINTS
1     4         200
2     2         150
3     1         100
4     3          80

Something like: "SELECT COUNT..."

tks a lot! Daniel

EDIT: It's not duplicated because I don't want to create a ranking. I want to count the position of the result. The answer proposed here fits 100%.


Solution

  • You could try counting the numers of rows with points >= at the points of playerid = 1

    select count(*)  from (
      SELECT PLAYERID
          , SUM(POINTS) AS POINTS 
      FROM TABLE1 
      GROUP BY PLAYERID 
    ) T 
    WHERE T.POINT >= (
      SELECT SUM(POINTS) 
      FROM TABLE1 
      WHERE PLAYERID = 1
    )