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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:05:30+00:00 2026-05-11T06:05:30+00:00

So I have a table as follows: ID_STUDENT | ID_CLASS | GRADE —————————– 1

  • 0

So I have a table as follows:

ID_STUDENT | ID_CLASS | GRADE -----------------------------    1       |    1     |  90    1       |    2     |  80    2       |    1     |  99    3       |    1     |  80    4       |    1     |  70    5       |    2     |  78    6       |    2     |  90    6       |    3     |  50    7       |    3     |  90 

I need to then group, sort and order them to give:

ID_STUDENT | ID_CLASS | GRADE | RANK ------------------------------------     2      |    1     |  99   |  1     1      |    1     |  90   |  2     3      |    1     |  80   |  3     4      |    1     |  70   |  4     6      |    2     |  90   |  1     1      |    2     |  80   |  2     5      |    2     |  78   |  3     7      |    3     |  90   |  1     6      |    3     |  50   |  2 

Now I know that you can use a temp variable to rank, like here, but how do I do it for a grouped set? Thanks for any insight!

  • 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. 2026-05-11T06:05:30+00:00Added an answer on May 11, 2026 at 6:05 am
    SELECT id_student, id_class, grade,    @student:=CASE WHEN @class <> id_class THEN 0 ELSE @student+1 END AS rn,    @class:=id_class AS clset FROM   (SELECT @student:= -1) s,   (SELECT @class:= -1) c,   (SELECT *    FROM mytable    ORDER BY id_class, id_student   ) t 

    This works in a very plain way:

    1. Initial query is ordered by id_class first, id_student second.
    2. @student and @class are initialized to -1
    3. @class is used to test if the next set is entered. If the previous value of the id_class (which is stored in @class) is not equal to the current value (which is stored in id_class), the @student is zeroed. Otherwise is is incremented.
    4. @class is assigned with the new value of id_class, and it will be used in test on step 3 at the next row.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To give a simple analogy, I have a table as follows: id (PK) |
I have table 1, all_countries, as follows- id | country ------------------ 1 | USA
I have a table set up as follows id origin destination carrier_id so typical
I have a query as follows: String SQL = insert into table (id, name)
I have a table as follows > RowID SessionID EventID RequestedURL Date > 1
I use JPA2 for the dao layer. Suppose i have a table as follows
I have a table like as follows: SoftwareName Count Country Project 15 Canada Visio
I have a HTML table as follows. I want retrieve row values from this
Suppose I have a table MEETING as follows M_DATE M_RACE_NO M_VENUE M_ATHLETE The primary
i have 2 tables, users and follows. table follows has a column named status.

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.