jeudi 13 août 2015

using SQL to rank scores

What is the correct way in SQL to add a value to a field that is an ordinal ranking.

I say "correct" because I think I need to pull the records in order, and update the field in sequence as I loop over them - only I know this isn't efficient - I know it should be able to be done strictly in the db engine.

Here's the scenario - I have 1000 students with test score averages thru the year.

I want to rank them highest to lowest, and store their ranking int the db, such that when the record is pulled (either singularly or in a group) the 'rank' comes with the record... in other words, yes, if i pull the WHOLE set, and order by avg_score DESC, I'll get the ranking, but it wont 'stick' with the record.

So how would I do that in SQL. Specifically MySQL 5.5

STUDENTS (table)
    id (primary key)
    name
    avg_score
    rank

Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire