• ORACLE逐行累计求和方法(OVER函数)


    sql over的作用及用法 
    1.RANK ( ) OVER ( [query_partition_clause] order_by_clause ) 
    DENSE_RANK ( ) OVER ( [query_partition_clause] order_by_clause ) 
    可实现按指定的字段分组排序,对于相同分组字段的结果集进行排序, 
    其中PARTITION BY 为分组字段,ORDER BY 指定排序字段

    2.over不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用。 
    其参数:over(partition by columnname1 order by columnname2) 
    含义:按columname1指定的字段进行分组排序,或者说按字段columnname1的值进行分组排序。

    解决实际问题:(实现统计功能中常用) 
    1)问题描述:比如查询记录有5行,每行记录有一个数值型的字段。第2行为第1、2行的和;第3行为第1、2、3行的和;第4行为第1、2、3、4行的和;后面依此类推…… 
    2)解决办法:使用Oracle自带的Over函数。 
    如下例子: 
    1.建测试表EMP 
    – Create table 
    create table employee 

    DEPTNO NUMBER(4), 
    ENAME VARCHAR2(20), 
    SAL NUMBER(10) 
    ); 
    2.插入测试数据 
    insert into employee (DEPTNO,ENAME,SAL) values (0001,’CLARK’,2450); 
    insert into employee (DEPTNO,ENAME,SAL) values (0002,’SMITH’,3000); 
    insert into employee (DEPTNO,ENAME,SAL) values (0003,’ALLEN’,1250); 
    insert into employee (DEPTNO,ENAME,SAL) values (0004,’JAMES’,950); 
    查询结果如下: 
    这里写图片描述

    3.编写SQL(用Over函数) 
    select deptno, 
    sal, 
    sum(sal) over (order by deptno) AccSal 
    from employee

    查询结果如下: 
    这里写图片描述

    未完待续…

  • 相关阅读:
    Spring05_基于注解的IOC和DI
    Spring02_基于XML的IOC
    Spring01_概述及程序的耦合
    设计模式六、单例模式
    设计模式五,建造者模式
    前后端分离异常统一处理
    vue qs.stringify 和JSON.stringify 区别
    设计模式四、抽象工厂模式
    设计模式三、工厂方法模式
    设计模式二、简单工厂模式——静态工厂模式
  • 原文地址:https://www.cnblogs.com/husam/p/9071427.html
Copyright © 2020-2023  润新知