【问题标题】:Error - not a single-group group function错误 - 不是单组群功能
【发布时间】:2021-02-03 10:23:30
【问题描述】:

我有这个问题:

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

但它显示错误“ORA-00937: not a single-group group function”。你知道为什么吗?

查询也略有不同,限制行数,如下所示:

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

它显示错误:“ORA-00933: SQL command not properly ended”。你知道为什么吗?谢谢!

【问题讨论】:

标签: sql oracle


【解决方案1】:

尝试使用子查询:

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;

我的猜测是您的 Oracle 版本可能不支持fetch

【讨论】:

    【解决方案2】:

    您不能在同一范围内嵌套聚合函数。 fetch 子句仅在 Oracle 开始版本 12c 中可用,并且您似乎正在运行早期版本。另一种方法是使用窗口函数。这将为您提供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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-15
      • 2020-05-20
      • 2018-11-13
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      相关资源
      最近更新 更多