//将当前行某列的值与前面所有行的此列值相加,即累计求和:

//方法一:

with t as(

     select 1 val from dual union all

     select 3 from dual union all

     select 5 from dual union all

     select 7 from dual union all

     select 9 from dual)

select val,

       sum(val)

       over (order by rownum rows between unbounded preceding and current row)

       sum_val

from t

group by rownum,val

order by rownum;

       VAL    SUM_VAL

---------- ----------

         1          1

         3          4

         5          9

         7         16

         9         25

//解析:

//sum(val)计算累积和;

//order by rownum 按照伪列rownum对查询的记录排序;

//between unbounded preceding and current row:定义了窗口的起点和终点;

//unbounded preceding:窗口的起点包括读取到的所有行;

//current row:窗口的终点是当前行,默认值,可以省略;

//

//方法二:

with cte_1 as(

     select 1 val from dual union all

     select 3 from dual union all

     select 5 from dual union all

     select 7 from dual union all

     select 9 from dual

     )

,cte_2 as(

    select rownum rn,val from cte_1

    )

select a.val , sum(b.val) sum_val

from cte_2 a , cte_2 b

where b.rn <= a.rn

group by a.val

/

//方法三:

//创建一个递归函数,求和

//f(n) = x + f(n-1)

create table t

as

select 1 id,1 val from dual union all

select 2,3 from dual union all

select 3,5 from dual union all

select 4,7 from dual union all

select 5,9 from dual

/

create or replace function fun_recursion(x in int)

return integer is

       n integer :=0;

begin

     select val into n

     from t

     where id=x;

     if x=1 then

        return n;

     else

         return n + fun_recursion(x-1);

     end if;

     exception

     when others then

          dbms_output.put_line(sqlerrm);

end fun_recursion;

/

select val,fun_recursion(id) sum_val from t;

       VAL    SUM_VAL

---------- ----------

         1          1

         3          4

         5          9

         7         16

         9         25

//  

oracle累计求和的更多相关文章

  1. SQL集合运算参考及案例(一):列值分组累计求和

    概述 目前企业应用系统使用的大多数据库都是关系型数据库,关系数据库依赖的理论就是针对集合运算的关系代数.关系代数是一种抽象的查询语言,是关系数据操纵语言的一种传统表达方式.不过我们在工作中发现,很多人 ...

  2. 数据可视化之DAX篇(十)在PowerBI中累计求和的两种方式

    https://zhuanlan.zhihu.com/p/64418286 假设有一组数据, 已知每一个产品贡献的利润,如果要计算前几名产品的贡献利润总和,或者每一个产品和利润更高产品的累计贡献占总体 ...

  3. 数据可视化之DAX篇(二十三)ALLEXCEPT应用示例:更灵活的累计求和

    https://zhuanlan.zhihu.com/p/67441847 累计求和问题,之前已经介绍过(有了这几个公式,你也可以快速搞定累计求和),主要是基于比较简单的情形,针对所有的数据进行累计求 ...

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

    1.RANK ( ) OVER ( [QUERY_PARTITION_CLAUSE] ORDER_BY_CLAUSE ) DENSE_RANK ( ) OVER ( [QUERY_PARTITION_ ...

  5. Oracle聚合求和和聚合求积(顺便解决BOM展开的问题)

    本文参考网址:http://www.itpub.net/thread-1020772-1-1.html 我们在日常的工作中,经常遇到了针对某一列的值,进行求和,求平均值,在一些特殊的业务场景下,我们需 ...

  6. oracle累积求和分析函数sum over的使用

    oracle sum()over函数的使用 over不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用. over函数的参数:over(partit ...

  7. Hive面试题——累计求和

    需求: 有如下访客访问次数统计表 t_access_times 访客 月份 访问次数 A 2015-01 5 A 2015-01 15 B 2015-01 5 A 2015-01 8 B 2015-0 ...

  8. Storm累计求和进群运行代码

    打成jar包放在主节点上去运行. import java.util.Map; import backtype.storm.Config; import backtype.storm.StormSubm ...

  9. Storm累计求和Demo并且在集群上运行

    打成jar包放在主节点上去运行. import java.util.Map; import backtype.storm.Config; import backtype.storm.StormSubm ...

随机推荐

  1. java类的封装、继承、多态

    一.封装(encapsulation) 封装性就是把类(对象)的属性和行为结合成一个独立的相同单位,并尽可能隐蔽类(对象)的内部细节,对外形成一个边界,只保留有限的对外接口使之与外部发生联系.封装的特 ...

  2. Google市场推广统计

    Google Play作为Android最大的应用市场,也存在这推广等常用的行为,那么如何统计呢,Google Analytics SDK或者其他的SDK都提供了方法,实际上是可以不需要任何sdk,完 ...

  3. Database(Mysql)发版控制二

    author:skate time:2014/08/18 Database(Mysql)发版控制 The Liquibase Tool related Database 一.Installation ...

  4. 自己的第一个android应用(天气)

    主界面代码 package com.example.weather; import android.os.Bundle; import android.app.Activity; import and ...

  5. mvc模式jsp+servel+jdbc oracle基本增删改查demo

    mvc模式jsp+servel+jdbc oracle基本增删改查demo 下载地址

  6. 1028: C语言程序设计教程(第三版)课后习题8.2

    Description求方程 的根,用三个函数分别求当b^2-4ac大于0.等于0.和小于0时的根,并输出结果.从主函数输入a.b.c的值.Inputa b cOutputx1=? x2=?Sampl ...

  7. 记录:sea.js和require.js配置 与 性能对比

    最近有点忙,很久无写博客,记录一下之前的配置require.js和sea.js的配置.(有误有望提出 require.js 文件目录 /app(项目使用js) /lib(require.js jq存放 ...

  8. PHP-语法(www.w3school.com.cn/php)

    写在前面: 假设系统里已安装PHP软件 PHP是一种脚本语言,执行PHP脚本后向浏览器返回纯HTML语言(即后台将.php文件的执行结果以纯HTML的形式返回到前端) ---------------- ...

  9. Delphi中取整函数Round的Bug解决

    Delphi中 Round函数有个Bug一旦参数是形如 XXX.5这样的数时如果 XXX 是奇数 那么就会 Round up如果 XXX 是偶数 那么就会 Round down例如 Round(17. ...

  10. HDU 1544 Palindromes(回文子串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1544 问题分析: 问题要求求出字符串的连续子串中的回文子串个数.首先,需要区分连续子串与子序列的区别. ...