zoukankan      html  css  js  c++  java
  • [Oracle]高效的SQL语句之分析函数(一)sum()

    实际应用中我们可以通过sum()统计出组中的总计或者是累加值,具体示例如下:

    1.创建演示表

     create   table  emp
     as
     select   *   from  scott.emp;
    
     alter   table  emp
     add   constraint  emp_pk
     primary   key (empno);
    
     create   table  dept
     as
     select   *   from  scott.dept;
    
     alter   table  dept
     add   constraint  dept_pk
     primary   key (deptno);

    2. sum()语句如下:

     select  deptno,
           ename,
           sal,
       -- 按照部门薪水累加(order by改变了分析函数的作用,只工作在当前行和前一行,而不是所有行)
             sum (sal)  over  (partition  by  deptno  order   by  sal) CumDeptTot,  
            sum (sal)  over  (partition  by  deptno) SalByDept,   -- 统计一个部门的薪水
             sum (sal)  over  ( order   by  deptno,sal) CumTot,   -- 所有雇员的薪水一行一行的累加
             sum (sal)  over  () TotSal   -- 统计总薪水
        from  emp
      order   by  deptno, sal

    3. 结果如下:

     10    MILLER    1300.00    1300    8750    1300    29025
    10    CLARK    2450.00    3750    8750    3750    29025
    10    KING    5000.00    8750    8750    8750    29025
    20    SMITH    800.00    800    10875    9550    29025
    20    ADAMS    1100.00    1900    10875    10650    29025
    20    JONES    2975.00    4875    10875    13625    29025
    20    SCOTT    3000.00    10875    10875    19625    29025
    20    FORD    3000.00    10875    10875    19625    29025
    30    JAMES    950.00    950    9400    20575    29025
    30    WARD    1250.00    3450    9400    23075    29025
    30    MARTIN    1250.00    3450    9400    23075    29025
    30    TURNER    1500.00    4950    9400    24575    29025
    30    ALLEN    1600.00    6550    9400    26175    29025
    30    BLAKE    2850.00    9400    9400    29025    29025
    好的代码像粥一样,都是用时间熬出来的
  • 相关阅读:
    迭代和列表生成式
    python递归函数
    python函数
    变量
    python第八课后整理
    python第八课
    python第七课
    python第六课
    python第五课
    微信端/企业微信端H5页面调试方法
  • 原文地址:https://www.cnblogs.com/jijm123/p/15761652.html
Copyright © 2011-2022 走看看