Not sure how to ask a followup on SO, but this is in reference to an earlier question: Fetch one row per account id from list
The query I’m working with is:
SELECT * FROM scores s1 WHERE accountid NOT IN (SELECT accountid FROM scores s2 WHERE s1.score < s2.score) ORDER BY score DESC
This selects the top scores, and limits results to one row per accountid; their top score.
The last hurdle is that this query is returning multiple rows for accountids that have multiple occurrences of their top score. So if accountid 17 has scores of 40, 75, 30, 75 the query returns both rows with scores of 75.
Can anyone modify this query (or provide a better one) to fix this case, and truly limit it to one row per account id?
Thanks again!
If you’re only interested in the accountid and the score, then you can use the simple GROUP BY query given by Paul above.
If you need other attributes from the scores table, then you can get other attributes from the row with a query like the following:
But this still gives multiple rows, in your example where a given accountid has two scores matching its maximum value. To further reduce the result set to a single row, for example the row with the latest gamedate, try this: