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 8145935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:49:26+00:00 2026-06-06T13:49:26+00:00

How to order the rows with respect to multiple columns such that the ordering

  • 0

How to order the rows with respect to multiple columns such that the ordering maintains the condition that if two people have the same details they get the same rank. I will give an example to illustrate :Here firstly ordering is done by score and in case of tie with penalty and still if tie exist both are given same rank and next person gets the adjusted rank.

###################
rank roll score penalty
 1    11    3     23
 2    12    3     20
 2    13    3     20
 2    14    3     20
 5    15    2     10

So the question is how to fill the rank column??If not possible in mysql what is the other alternative??

  • 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-06-06T13:49:28+00:00Added an answer on June 6, 2026 at 1:49 pm

    Test data:

    /*
    drop table test;
    create table test (roll int, score int, penalty int);
    insert into test (roll, score, penalty) values (11, 3, 23), (12, 3, 20), (13, 3, 20), (14,3,20), (15, 2, 10);
    */
    

    And here it comes:

    ALTER TABLE test ADD COLUMN `rank` int FIRST;
    CREATE TEMPORARY TABLE tmp_test LIKE test;
    INSERT INTO tmp_test (`rank`, roll, score, penalty)
    SELECT cast(q.`rank` as unsigned integer) as `rank`, roll, score, penalty FROM (
    SELECT IF(@prev != CONCAT(sq.score, '_', sq.penalty), @rownum:=@rownum2, @rownum) AS rank, 
    @prev:=CONCAT(sq.score, '_', sq.penalty), 
    @rownum2:=@rownum2 + 1,
    sq.*
    FROM (
      SELECT
      roll, score, penalty
      FROM
      test
      , (SELECT @rownum:=0, @prev:='', @rownum2:=1) r 
      ORDER BY score DESC, penalty DESC
    ) sq
    ) q;
    
    UPDATE test t INNER JOIN tmp_test tt ON t.roll = tt.roll AND t.score = tt.score AND t.penalty = tt.penalty
    SET t.rank = tt.rank;
    /*optionally...*/    
    DROP TABLE tmp_test;
    

    You need to work with a temporary table here, because you can’t update the table you are reading from. The temporary table will be automatically deleted when the session ends.

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

Sidebar

Related Questions

I have some code that creates a CSV file. The order of the rows
I have a quadratic matrix(two-dimensional dynamic array of pointers) and need to change rows/columns
I have a table like: I have to order the rows in function of
I have an Access database of 4M rows, each representing an individual customer order.
I have several tables that get JOINed together to form a table with columns
Is it possible to order by multiple rows? I want my users to be
I have a table that has a fixed layout of rows. Each row has
I need to be able to determine the order which rows have been inserted
I have a pretty simple question: Is it possible to order the rows retrieved
I have two data.frames in R, one of which has two columns and of

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.