I have a competition where reps can many different stores, stores have points based on people entering the store.
What I need to do is select the rank of a store (where id = x), based on the points value, if a store is tied, then the rank should be the same (i.e. two stores in joint first)
I then need to select the rank of a rep, based on the sum of the reps stores scores.
Stores
id name points rep_id
1 Regents Street 1501 3
2 Oxford Street 1500 2
3 Carnaby Street 1499 3
4 Edward Street 1499 1
5 Edward Street 1499 4
Reps
id name
1 John Smith
2 John Doe
3 Joe Smit
3 Lou Jackson
Store leaderboard:
1st - 1, Regents Street
2nd - 2, Oxford Street
3rd - 3,4,5
Rep leaderboard:
1st - 3, Joe Smit (3000)
2nd - 2, John Doe (1500)
3rd - 1,4 (1499)
I have a query to get the store ranks, but it doesn't work with ties.
SELECT id,
name,
points,
FIND_IN_SET(points,
(SELECT GROUP_CONCAT(points
ORDER BY points DESC)
FROM stores)) AS rank
FROM stores
WHERE id = 1
Thanks
I would normally use variables for this type of ranking:
Another approach is a correlated subquery:
Both these queries actually implement a
dense_rank()
rather than arank()
. It is unclear from your question what you actually want. And, they can be modified for a rank as well.EDIT:
If you need this for reps rather than stores (which does seem to be buried near the beginning of the question):
I think this will work in MySQL: