对于以下sql命令
select h.hacker_id
,h.name
,challenges_created
from hackers h
inner join (
select hacker_id
,count(*) as challenges_created /* line 1 */
from challenges
group by hacker_id
order by 2 desc
) tmp on h.hacker_id = tmp.hacker_id
order by challenges_created desc
,h.hacker_id;
到目前为止还不错
只要我尝试将max(count(*)) as maximum
添加到第1行,就会出现错误:
不是单组组功能
这是它给出错误的代码:
select h.hacker_id
,h.name
,challenges_created
from hackers h
inner join (
select hacker_id
,count(*) as challenges_created
,max(count(*)) as maximum
from challenges
group by hacker_id
) tmp on h.hacker_id = tmp.hacker_id
order by challenges_created desc
,h.hacker_id;
到目前为止,我基本上对获得最大数量即challenges created
的最大数量感兴趣。
我是sql的新手,请帮助和帮助。提前致谢。是的,当然!我知道最近有很多这样的问题被问到,但没有一个符合我的情况,这就是为什么我要再问一次。
答案 0 :(得分:0)
试试这个:
with x as (
select h.hacker_id, h.name, count(*) challenges_created
from hackers h
inner join challenges on h.hacker_id = challenges.hacker_id
group by h.hacker_id, h.name
)
select x.*,
(select max(challenges_created) from x) as "max"
from x
order by challenges_created desc, hacker_id;