Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You'll need to use a subquery to pull the score out. </p> <pre><code>select distinct name, max(level) as level, (select max(score) from highscores h2 where h2.name = h1.name and h2.level = h1.level) as score from highscores h1 group by name order by level desc, score desc </code></pre> <p>Cheers,</p> <p>Eric</p> <p>It irks me that I didn't take the time to explain why this is the case when I posted the answer, so here goes:</p> <p>When you pull back everything (*), and then the max level, what you'll get is each record sequentially, plus a column with the max level on it. Note that you're not grouping by score (which would have given you Bob 2 41, and Bob 3 21--two records for our friend Bob).</p> <p>So, how the heck do we fix this? You need to do a subquery to additionally filter your results, which is what that (select max(score)...) is. Now, for each row that reads Bob, you will get his max level (3), and his max score at that level (21). But, this still gives us however many rows Bob has (e.g.-if he has 5 rows, you'll get 5 rows of Bob 3 21). To limit this to only the top score, we need to use a DISTINCT clause in the select statement to only return unique rows.</p> <p>UPDATE: Correct SQL (can't comment on le dorfier's post):</p> <pre><code>SELECT h1.Name, h1.Level, MAX(h1.Score) FROM highscores h1 LEFT OUTER JOIN highscores h2 ON h1.name = h2.name AND h1.level &lt; h2.level LEFT OUTER JOIN highscores h3 ON h1.name = h3.name AND h2.level = h3.level AND h1.score &lt; h3.score WHERE h2.Name IS NULL AND h3.Name IS NULL GROUP BY h1.Name, h1.Level </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload