Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 569553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:18:27+00:00 2026-05-13T13:18:27+00:00

I have a huge list ranked by various values (eg. scores) So I grab

  • 0

I have a huge list ranked by various values (eg. scores)

So I grab the list ordered by these values:

players = Player.objects.order_by('-score', '-karma')

I would like to:

  • Grab a player and get the neighbouring players

P1 score:123

P2 score:122

YOU! score:110

P3 score:90

P2 score:89

  • Grab the position!

You are ranked #1234 for score

You are ranked #9876 for karma


Help would be very much appreciated. thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:18:27+00:00Added an answer on May 13, 2026 at 1:18 pm

    To get the ranking of the user:

    (SELECT * FROM (
      SELECT
        RANK() OVER (ORDER BY Score desc ,Karma desc) AS ranking,
        Id,
        Username,
        Score, karma
      FROM Players 
    ) AS players_ranked_by_score
    where Id = id_of_user 
    

    Where id_of_user is the parameter containing the id of the current player. To get the neighboring players and the current user:

    (SELECT * FROM (
      SELECT
        RANK() OVER (ORDER BY Score desc ,Karma desc) AS ranking,
        Id,
        Username,
        Score, karma
      FROM Players 
    ) AS all_players_ranked
    where ranking >= player_ranking - 2 and ranking <= player_ranking + 2;
    

    Where player_ranking is the ranking obtained from the query above.

    Hope it helps!

    Update: MySQL does not have a rank() function (MS SQL, Oracle, Postgres have one). I looked around and I got this link explaining how to do ranking in MySQL: http://www.artfulsoftware.com/infotree/queries.php?&bw=1024#460.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have huge list (200000) of strings (multi word). I want to group these
I have a huge checkbox list with different levels and I would like to
I have a huge Ping list and want to clean it. Using PHP I
I have a huge autocomplete field with a list of exams. This field prepends
I have a huge array list which contains 1000 entries out of which one
I have a huge list of URL's, in the format: http://www.example.com/dest/uk/bath/ http://www.example.com/dest/aus/sydney/ http://www.example.com/dest/aus/ http://www.example.com/dest/uk/
I have a huge list of data, more than 1M records in a form
I have a huge list of tuples in this format. The second field of
I have a persistent object that has a huge list of child elements. I'm
I have a huge list containing many strings like: ['xxxx','xx','xy','yy','x',......] Now I am looking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.