簡體   English   中英

MYSQL 第 1 行的錯誤 1111 (HY000):組 function 的使用無效。如何修復此錯誤?

[英]MYSQL ERROR 1111 (HY000) at line 1: Invalid use of group function. How can I fix this error?

select h.hacker_id, name, count(challenge_id) as total from Challenges c inner join Hackers h on h.hacker_id=c.hacker_id group by h.hacker_id, name 
having total not in 
(select count(challenge_id) as cnt from Challenges c where c.hacker_id!=h.hacker_id
group by c.hacker_id
having cnt!= (select max(count(challenge_id)) from Challenges group by hacker_id))
order by total desc, h.hacker_id

這是我的 MySql 代碼,我收到一個錯誤:第 1 行的錯誤 1111 (HY000):組 function 的使用無效。

我不知道這一行有什么問題: (select max(count(challenge_id)) from Challenges group by hacker_id)我該如何解決這個錯誤?

我想解決的問題鏈接: https://www.hackerrank.com/challenges/challenges/problem

您不能將 2 個分組函數與 1 個分組依據一起使用。

代替

select max(count(challenge_id)) from Challenges group by hacker_id)

你可以做

select max(cnt_challenge) from (select count(challenge_id) as cnt_challenge from Challenges group by hacker_id)) 

問題是max(count(). However, I would solve it using

having cnt <> (select count(*)
               from challenges
               group by hacker_id
               order by count(*) desc
               limit 1
              )

也就是說,使用 window 函數可能會更好地編寫此查詢。 然而,如果沒有樣本數據、期望的結果以及對查詢應該做什么的明確解釋,就很難提出具體的建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM