-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumJoinMax.sql
More file actions
10 lines (10 loc) · 858 Bytes
/
SumJoinMax.sql
File metadata and controls
10 lines (10 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
SELECT joint.hack, h.name, sum(joint.max_score) total_score --Return the values, hacker_id, name and sum of max scores
FROM (SELECT s.hacker_id as hack, max(s.score) max_score -- Create a nested select to return the id and max score
FROM SUBMISSIONS s
WHERE s.score >0 -- Ensure value is greater than zero
GROUP BY s.hacker_id, s.challenge_id -- Aggregate by hacker_id, and challenge_id
) joint
INNER JOIN HACKERS h
ON joint.hack = h.hacker_id -- Join to the hacker table to get names
GROUP BY joint.hack, h.name -- Group by both the hacker_id and name. Should be one to one relation
ORDER BY total_score desc, joint.hack -- Order by appropriate value