zoukankan      html  css  js  c++  java
  • rollup&&cube

    group by 擴展 rollup&&cube

    --按job分組計算不同job的匯總工資

      SELECT job, SUM (sal)
        FROM emp
    GROUP BY job
    ORDER BY job;

    --向rollup傳遞一列

      SELECT job, SUM (sal)
        FROM emp
    GROUP BY ROLLUP (job)
    ORDER BY job;

    image

    --向rollup傳遞多列,根據job小計

      SELECT job, deptno, SUM (sal)
        FROM emp
    GROUP BY ROLLUP (job, deptno)
    ORDER BY job, deptno;

    image

    --修改傳遞給rollup的列的位置,根據部門小計

      SELECT deptno, job, SUM (sal)
        FROM emp
    GROUP BY ROLLUP (deptno, job)
    ORDER BY deptno, job;

    image

    --rollup與其它聚合函數使用,任何聚合函數都可以和rollup一起使用

      SELECT deptno, job, ROUND (AVG (sal), 2)
        FROM emp
    GROUP BY ROLLUP (deptno, job)
    ORDER BY deptno, job;

    image

    --使用cube子句

      SELECT deptno, job, SUM (sal)
        FROM emp
    GROUP BY CUBE (deptno, job)
    ORDER BY deptno, job;

    image

    --cube改變順序

      SELECT job, deptno, SUM (sal)
        FROM emp
    GROUP BY CUBE (job, deptno)
    ORDER BY job, deptno;

    image

    --grouping 返回0 1

      SELECT GROUPING (job), job, SUM (sal)
        FROM emp
    GROUP BY ROLLUP (job)
    ORDER BY job;

    image

    --轉換grouping返回值為有意義的值

      SELECT CASE GROUPING (job)
                       WHEN 1 THEN 'ALL JOBS'
                       ELSE job
                  END AS job,
                  SUM (sal)
        FROM emp
    GROUP BY ROLLUP (job)
    ORDER BY SUM (sal);

    image

    --grouping多列
      SELECT
      CASE GROUPING (deptno) WHEN 1 THEN  40 ELSE deptno END AS deptno,
      CASE GROUPING (job)      WHEN 1 THEN 'ALL JOBS'    ELSE job      END AS job,
             SUM (sal)
        FROM emp
    GROUP BY ROLLUP (deptno,job)
    ORDER BY deptno,job;

    image


    --cube與grouping一起使用
      SELECT
      CASE GROUPING (deptno) WHEN 1 THEN  40 ELSE deptno END AS deptno,
      CASE GROUPING (job)      WHEN 1 THEN 'ALL JOBS'    ELSE job      END AS job,
             SUM (sal)
        FROM emp
    GROUP BY cube (deptno,job)
    ORDER BY deptno,job;

    image

    --使用grouping sets只顯示小計結果
      SELECT deptno,job,
                  SUM (sal)
        FROM emp
    GROUP BY grouping sets (deptno,job)
    ORDER BY deptno,job;

    image

  • 相关阅读:
    求连续子数组的最大和
    【LeetCode练习题】Gas Station
    再来看看快速排序
    【LeetCode练习题】First Missing Positive
    【LeetCode练习题】Merge Sorted Array
    Hdu 2089-不要62 数位dp
    Tsinsen A1517. 动态树 树链剖分,线段树,子树操作
    Bzoj 3505: [Cqoi2014]数三角形 数论
    Poj 3695-Rectangles 矩形切割
    Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序
  • 原文地址:https://www.cnblogs.com/guilingyang/p/6368090.html
Copyright © 2011-2022 走看看