问题
I've been working on a HackerRank SQL question, "Challenges".
This is the problem:
Julia asked her students to create some coding challenges. Write a query to print the hacker_id, name, and the total number of challenges created by each student. Sort your results by the total number of challenges in descending order. If more than one student created the same number of challenges, then sort the result by hacker_id. If more than one student created the same number of challenges and the count is less than the maximum number of challenges created, then exclude those students from the result.
This question stumped me, and I was searching online to see how other people have solved it, when I came across this Github here, who proposed a really elegant solution.
SELECT c.hacker_id,
h.name,
COUNT(c.challenge_id) AS cnt
FROM Hackers AS h
JOIN Challenges AS c ON h.hacker_id = c.hacker_id
GROUP BY c.hacker_id, h.name
HAVING cnt = (SELECT COUNT(c1.challenge_id)
FROM Challenges AS c1
GROUP BY c1.hacker_id
ORDER BY COUNT(*) DESC
LIMIT 1)
OR
cnt NOT IN (SELECT COUNT(c2.challenge_id)
FROM Challenges AS c2
GROUP BY c2.hacker_id
HAVING c2.hacker_id <> c.hacker_id)
ORDER BY cnt DESC, c.hacker_id;
I understand how the poster ensured that we'd only get the maximum number of challenges in the first condition of the HAVING, but as for the second part, I don't really understand. I'm not sure why the second condition of the HAVING query works: HAVING c2.hacker_id <> c.hacker_id
Won't c2.hacker_id = c.hacker_id always?
回答1:
if you equalize them (c2.hacker_id = c.hacker_id), the results relates with same hacker_id. on the inequality condition it counts by not including hacker_id itself. it means count number of challenges whose hacker_id is not equal to hacker_id of the outer query and exempt those counts from main query.
来源:https://stackoverflow.com/questions/54857386/meaning-of-this-line-having-c2-hacker-id-c-hacker-id