I would like to give each player a rank / position based on the values of other columns, 1 being the best position. Each prize or penalty is worth a different amount. Can this be done in SQL and how would I go about putting this into a query? Here is the ratio that i had in mind:
((gold * 8) + (silver * 4) + (bronze * 2)) – ((disq * 4) + penalty) = points
The ‘points’ are insignificant, ideally I would be able to update the query if the ratio changes.
id | name | gold | silver | bronze | disq | penalty | rank |
---|---|---|---|---|---|---|---|
1 | ben | 1 | 4 | 8 | 5 | ||
2 | kim | 4 | 1 | 3 | 1 | 2 | |
3 | sarah | 2 | 2 | 1 | |||
4 | matt | 10 | 2 | 1 | 3 | ||
5 | jane | 2 | 3 | 5 | 1 |
2
Answers
you can try this.
You could use the rank window function as follow:
https://dbfiddle.uk/3FiKkmVA
Note , you need to use coalesce for the null values
Or you could update your table with the rank
https://dbfiddle.uk/ckwNhf6y