grouping sets

grouping sets可以合并多个分组的结果

SQL> select deptno,avg(sal) from emp group by deptno;

    DEPTNO   AVG(SAL)
---------- ----------
        10 2916.66667
        20       2175
        30 1566.66667

SQL> select job,avg(sal) from emp group by job;

JOB         AVG(SAL)
--------- ----------
ANALYST         3000
CLERK         1037.5
MANAGER   2758.33333
PRESIDENT       5000
SALESMAN        1400

SQL> select deptno,job,avg(sal) from emp group by grouping sets( deptno,job);

    DEPTNO JOB         AVG(SAL)
---------- --------- ----------
           ANALYST         3000
           CLERK         1037.5
           MANAGER   2758.33333
           PRESIDENT       5000
           SALESMAN        1400
        10           2916.66667
        20                 2175
        30           1566.66667

8 rows selected.

使用grouping sets可同时得到使用sal,和deptno统计的结果

你可能感兴趣的:(oracle每日学习)