Hackerank挑战MySQL查询返回错误输出

j8ag8udp  于 2022-12-10  发布在  Mysql
关注(0)|答案(1)|浏览(97)

从这个黑客问题https://www.hackerrank.com/challenges/challenges/problem?isFullScreen=true
为什么我的查询不会返回完成最大挑战次数的所有黑客的名字?
我的查询给出了每个黑客的所有名字和所有挑战。

This is my query down below

SELECT am.name,am.names_appeared from (

    SELECT Hackers.hacker_id,Hackers.name,Count(name) as names_appeared from Hackers 
    INNER JOIN Challenges on Hackers.hacker_id = Challenges.hacker_id GROUP BY Hackers.hacker_id,Hackers.name
    
)am
GROUP BY am.name,am.names_appeared
HAVING am.names_appeared = MAX(am.names_appeared)

`
`
jgwigjjp

jgwigjjp1#

在您的内部查询中添加一个排名,并在您的外部查询中仅保留排名=1。

SELECT am.name,am.names_appeared from (

    SELECT Hackers.hacker_id,
           Hackers.name,
           Count(name) as names_appeared,
       rank() Over (Partition By Hackers.hacker_id,Hackers.name
        Order by Count(name) Desc) as hackerrank
    from Hackers 
      INNER JOIN Challenges
        on Hackers.hacker_id = Challenges.hacker_id
    GROUP BY Hackers.hacker_id,Hackers.name

)am
XXXX lose this line XXXX Group BY am.name,am.names_appeared
XXXX lose this line XXXX HAVING am.names_appeared = MAX(am.names_appeared)
Where hackerank=1

相关问题