Meaning of this line: HAVING c2.hacker_id <> c.hacker.id?

一世执手 提交于 2021-02-11 13:32:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!