• sum()over()和count()over()分析函数


    创建测试表

    create table test(sales_id varchar2(2),sales varchar2(10),dest varchar2(10),dept varchar2(10),revenue number);

    插入测试数据

    insert into test values('11','smith','hangzhou','市场',1000);
    insert into test values('12','smith','wenzhou','市场',2000);
    insert into test values('13','allen','wenzhou','渠道',3000);
    insert into test values('14','allen','wenzhou','渠道',4000);
    insert into test values('15','jekch','shanghai','渠道',2500);
    insert into test values('11','smith','hangzhou','市场',1000);
    insert into test values('12','smith','wenzhou','市场',2000);
    commit;

    查看表记录

    SQL> select * from test;
    SALES_ID   SALES       DEST       DEPT     REVENUE
    -------- ---------- ---------- ---------- ----------
      11       smith      hangzhou    市场       1000
      12       smith      wenzhou     市场       2000
      13       allen      wenzhou     渠道       3000
      14       allen      wenzhou     渠道       4000
      15       jekch      shanghai    渠道       2500
      11       smith      hangzhou    市场       1000
      12       smith      wenzhou     市场       2000
    SQL> select sales_id,sales,dest,dept,revenue,sum(revenue) over() as 总销售额 from test;
    SALES_ID   SALES       DEST       DEPT      REVENUE   总销售额
    -------- ---------- ---------- ---------- ---------- ----------
       11      smith      hangzhou     市场        1000      15500
       12      smith      wenzhou      市场        2000      15500
       13      allen      wenzhou      渠道        3000      15500
       14      allen      wenzhou      渠道        4000      15500
       15      jekch      shanghai     渠道        2500      15500
       11      smith      hangzhou     市场        1000      15500
       12      smith      wenzhou      市场        2000      15500

    按照sales_id order by排序计算递加的销售总额

    SQL> select sales_id,sales,dest,dept,revenue,sum(revenue)over(order by sales)递加销售总额 from test;
    SALES_ID    SALES      DEST       DEPT      REVENUE  递加销售总额
    -------- ---------- ---------- ---------- ---------- ------------
       14       allen     wenzhou     渠道        4000      7000
       13       allen     wenzhou     渠道        3000      7000
       15       jekch     shanghai    渠道        2500      9500
       11       smith     hangzhou    市场        1000      15500
       12       smith     wenzhou     市场        2000      15500
       12       smith     wenzhou     市场        2000      15500
       11       smith     hangzhou    市场        1000      15500
    
    7 rows selected
    SQL> select sales_id,sales,dest,dept,revenue,sum(revenue)over(partition by sales_id) 分组销售总额 from test;
    SALES_ID   SALES       DEST       DEPT     REVENUE    分组销售总额
    -------- ---------- ---------- ---------- ---------- ------------
       11      smith      hangzhou    市场       1000         2000
       11      smith      hangzhou    市场       1000         2000
       12      smith      wenzhou     市场       2000         4000
       12      smith      wenzhou     市场       2000         4000
       13      allen      wenzhou     渠道       3000         3000
       14      allen      wenzhou     渠道       4000         4000
       15      jekch      shanghai    渠道       2500         2500

    sales_id进行分组,然后分组求sum

    SQL> select sales_id,sales,dest,dept,revenue,sum(revenue)over(partition by sales order by sales_id) 分组递加销售总额 from test;
    SALES_ID    SALES      DEST       DEPT     REVENUE    分组递加销售总额
    -------- ---------- ---------- ---------- ---------- ----------------
       13       allen     wenzhou     渠道       3000          3000
       14       allen     wenzhou     渠道       4000          7000
       15       jekch     shanghai    渠道       2500          2500
       11       smith     hangzhou    市场       1000          2000
       11       smith     hangzhou    市场       1000          2000
       12       smith     wenzhou     市场       2000          6000
       12       smith     wenzhou     市场       2000          6000

    sales进行分组,然后分组内递加sum。

    看下面的count()over()分析函数的使用

    SQL> select sales_id,sales,count(*)over()求总计数,
                   count(*)over(order by sales_id)递加求计数,
                   count(*)over(partition by sales_id)分组求计数,
                   count(*)over(partition by sales_id order by sales)分组递加求计数
         from test;    
    SALES_ID    SALES     求总计数   递加求计数   分组求计数  分组递加求计数
    -------- ---------- ---------- ---------- ---------- --------------
      11        smith        7         2           2           2
      11        smith        7         2           2           2
      12        smith        7         4           2           2
      12        smith        7         4           2           2
      13        allen        7         5           1           1
      14        allen        7         6           1           1
      15        jekch        7         7           1           1

    本文转自:http://blog.itpub.net/25362835/viewspace-1057319/

  • 相关阅读:
    springboot 文件上传和下载
    spring-boot-starter-actuator不起作用
    spring boot使用AOP切面编程
    Map四种获取key和value值的方法,以及对map中的元素排序(转)
    fastjson使用-- @JSONField使用(转)
    SpringDataJpa——JpaRepository查询功能(转)
    spring接口文档注解:@ApiOperation(转)
    一些专栏
    Redis的那些最常见面试问题(转)
    Java面试通关要点汇总集
  • 原文地址:https://www.cnblogs.com/dreammyle/p/4691486.html
Copyright © 2020-2023  润新知