sum() over()

SQL> select deptno,empno,sal,sum(sal) over (order by sal) from emp order by 3;

    DEPTNO      EMPNO        SAL SUM(SAL)OVER(ORDERBYSAL)
---------- ---------- ---------- ------------------------
        20       7369        800                      800
        30       7900        950                     1750
        20       7876       1100                     2850
        30       7521       1250                     5350
        30       7654       1250                     5350
        10       7934       1300                     6650
        30       7844       1500                     8150
        30       7499       1600                     9750
        10       7782       2450                    12200
        30       7698       2850                    15050
        20       7566       2975                    18025
        20       7788       3000                    24025
        20       7902       3000                    24025
        10       7839       5000                    29025


SQL>  select deptno,empno,sal,sum(sal) over (partition by deptno order by sal) from emp order by 3;

    DEPTNO      EMPNO        SAL SUM(SAL)OVER(PARTITIONBYDEPTNOORDERBYSAL)
---------- ---------- ---------- -----------------------------------------
        20       7369        800                                       800
        30       7900        950                                       950
        20       7876       1100                                      1900
        30       7654       1250                                      3450
        30       7521       1250                                      3450
        10       7934       1300                                      1300
        30       7844       1500                                      4950
        30       7499       1600                                      6550
        10       7782       2450                                      3750
        30       7698       2850                                      9400
        20       7566       2975                                      4875
        20       7902       3000                                     10875
        20       7788       3000                                     10875
        10       7839       5000                                      8750

SQL>  select deptno,empno,sal,sum(sal) over (partition by deptno) from emp order by 1;

    DEPTNO      EMPNO        SAL SUM(SAL)OVER(PARTITIONBYDEPTNO)
---------- ---------- ---------- -------------------------------
        10       7782       2450                            8750
        10       7839       5000                            8750
        10       7934       1300                            8750
        20       7566       2975                           10875
        20       7902       3000                           10875
        20       7876       1100                           10875
        20       7369        800                           10875
        20       7788       3000                           10875
        30       7521       1250                            9400
        30       7844       1500                            9400
        30       7499       1600                            9400
        30       7900        950                            9400
        30       7698       2850                            9400
        30       7654       1250                            9400

SQL> select ename,sal,sum(case when ename = 'MILLER' then sal else -sal end) over (order by sal,empno ) as running_diff from emp where deptno =10;

ENAME             SAL RUNNING_DIFF
---------- ---------- ------------
MILLER           1300         1300
CLARK            2450        -1150
KING             5000        -6150


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值