如何解决oracle sql中的单分组问题

Posted

技术标签:

【中文标题】如何解决oracle sql中的单分组问题【英文标题】:how to solve a single-grouping problem in oracle sql 【发布时间】:2020-01-31 03:21:47 【问题描述】:

我正在尝试使用 oracle 中提供的 EMP 表来练习透视,方法是选择 job 作为列,ename 作为值。 这是我目前得到的查询。

     select distinct job, count(job) from emp group by job ... (A)
     -- Result)
     -- CLERK 4 
     -- SALESMAN 4
     -- PRESIDENT 1
     -- MANAGER 3
     -- ANALYST 2
    select t1.ename, t2.ename, t3.ename, t4.ename, t5.ename
    from
        (select ename, rank() over (order by ename) as n from emp where job = 'CLERK') t1, 
        (select ename, rank() over (order by ename) as n from emp where job = 'SALESMAN') t2, 
        (select ename, rank() over (order by ename) as n from emp where job = 'PRESIDENT') t3, 
        (select ename, rank() over (order by ename) as n from emp where job = 'MANAGER') t4, 
        (select ename, rank() over (order by ename) as n from emp where job = 'ANALYST') t5, 
        (select ename, rank() over (order by ename) as n from emp where job = 
        (select job ... (C)
        from emp 
        group by job
        having count(*) = (
            select max(count(*))
            from emp 
            group by job))
    ) t6 
    where t6.n = t1.n(+)
        and t6.n = t2.n(+)
        and t6.n = t3.n(+)
        and t6.n = t4.n(+)
        and t6.n = t5.n(+)
    order by t6.n;
    output: ORA-00937: not a single-group group function ... (B)

但是输出是 (B),因为从 (A) 中可以看出,有两个工作(文员、销售员)的值均为 4。

问题) 有没有办法修改(C),所以它只会选择一个作业,尽管(A),因为我只想为所有作业创建足够的列以在旋转后适应?

希望我说清楚了。

* 完成查询 *

  select t1.ename "Clerk", t2.ename "Salesman", t3.ename "President", t4.ename "Manager", t5.ename "Analyst"
            from
                (select ename, rank() over (order by ename) as n from emp where job = 'CLERK') t1, 
                (select ename, rank() over (order by ename) as n from emp where job = 'SALESMAN') t2, 
                (select ename, rank() over (order by ename) as n from emp where job = 'PRESIDENT') t3, 
                (select ename, rank() over (order by ename) as n from emp where job = 'MANAGER') t4, 
                (select ename, rank() over (order by ename) as n from emp where job = 'ANALYST') t5, 
                (select ename, rank() over (order by ename) as n from emp where job = 
                    (select job
                    from
                        (select *
                        from (
                            select job, count(job)
                            from emp
                            group by job
                            order by count(job) desc) 
                        where rownum = 1))) t6
                where t6.n = t1.n(+)
                and t6.n = t2.n(+)
                and t6.n = t3.n(+)
                and t6.n = t4.n(+)
                and t6.n = t5.n(+)
            order by t6.n;

【问题讨论】:

【参考方案1】:

如果您使用的是 oracle 12c 或更高版本,您可以使用FETCH 子句,如下所示:

select job, count(job) 
  from emp
group by job 
ORDER BY COUNT(JOB) DESC
FETCH FIRST ROW ONLY

如果您使用的是较低版本的 oracle,则可以使用 analytical function,如下所示:

Select job, cnt from
(select job, count(job) cnt, row_number() over (order by count(job) desc) as rn
  from emp
 group by job)
Where rn = 1

干杯!!

【讨论】:

您的查询肯定有效,但我无法将其与上述内容结合起来,但我想这是我的工作。我应该指定我正在使用的版本。谢谢!

以上是关于如何解决oracle sql中的单分组问题的主要内容,如果未能解决你的问题,请参考以下文章

sql 分组 求累计值

SQL:如何取得分组中某个字段为最大值的那些记录

如何解决 Lumen/Laravel 中的单例?

如何解决 Oracle Apex 中的无效列错误?

如何在不使用 GROUP BY 或 PARTITION BY 的情况下对 Oracle SQL 中的数据进行分组

如何在不进行分组的情况下在Oracle中向SQL结果添加虚拟行