wkilc
Glancing back at my query now I realise a bug, which I've corrected. Basically, it's possible for students other than those Superman is judging to have scores the same as Superman's students' scores; you don't want them to be leaking into the list of "tied" students, so both branches need judge = 'superman'
filters.
So, because I'm the sort of person to get annoyed by repetition, I now see that every mention of the_table
is filtered by judge = 'superman'
and therefore:
WITH by_judge AS (SELECT id, student, score
FROM the_table
WHERE judge = 'superman'),
ties AS (SELECT score
FROM by_judge
GROUP BY score
HAVING count(score) > 1)
SELECT id, student, score, true AS tied
FROM by_judge WHERE score IN (SELECT score FROM ties)
UNION
SELECT id, student, score, false AS tied
FROM by_judge WHERE score NOT IN (SELECT score FROM ties)
ORDER BY score
And in fact I suspect that the UNION
could be eliminated by putting the IN
check into the tied
column directly: SELECT id, student, score, score IN (SELECT score FROM ties) AS tied