Skip to content

2820. Election Results 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
WITH 
  VoterToValue AS (
    SELECT
      voter,
      1 / COUNT(*) AS value
    FROM Votes
    GROUP BY voter
  ),
  RankedCandidates AS (
    SELECT
      candidate,
      RANK() OVER(ORDER BY SUM(value) DESC) AS `rank`
    FROM Votes
    INNER JOIN VoterToValue
      USING (voter)
    WHERE candidate IS NOT NULL
    GROUP BY 1
  )
SELECT candidate
FROM RankedCandidates
WHERE `rank` = 1
ORDER BY 1;