简体   繁体   English

错误 - 不是单组组功能

[英]Error - not a single-group group function

I have this query:我有这个查询:

select employee_id, max(count(employee_id))
from hr.job_history 
group by employee_id;
                

But it showns an error " ORA-00937: not a single-group group function ".但它显示了一个错误“ ORA-00937: not a single-group group function ”。 Do you know why?你知道为什么吗?

Also doing the query a little different, limiting the rows, like this:也做一些不同的查询,限制行,像这样:

select count(employee_id) 
from hr.job_history 
group by employee_id order by count(employee_id) desc fetch first 2 rows only;

It shows an error : " ORA-00933: SQL command not properly ended ".它显示错误:“ ORA-00933: SQL command not properly ended ”。 Do you know why?你知道为什么吗? Thanks!谢谢!

Try using a subquery:尝试使用子查询:

select e.*
from (select employee_id, count(employee_id)
      from hr.job_history 
      group by employee_id
      order by count(*) desc
     ) e
where rownum = 1;

My guess is that your version of Oracle may not support fetch .我的猜测是您的 Oracle 版本可能不支持fetch

You can't nest aggregate functions in the same scope.您不能在同一范围内嵌套聚合函数。 The fetch clause is available in Oracle starting version 12c only, and you seem to be running an earlier version. fetch子句仅在 Oracle 版本 12c 中可用,而且您似乎运行的是早期版本。 An alternative uses window functions.另一种方法是使用窗口函数。 This gives you the employee(s) with most rows in job_history , including ties if any:这为您提供了job_history最多行的job_history ,包括关系(如果有):

select *
from (
    select employee_id, count(employee_id) cnt,
        rank() over(order by count(employee_id) desc) rn
    from hr.job_history 
    group by employee_id
) t
where rn = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM