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

//方法一:

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. spring集成mongodb封装的简单的CRUD

    1.什么是mongodb         MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. mongoDB MongoDB是一个介 ...

  2. navicat重新系统丢失libmysql_e

    解决方法: 1把libmysql_e拷贝到c盘的Windows的system文件夹

  3. 关于iOS8上本地通知接收不到的问题

    在iOS8系统开发使用本地通知时,会出现如下的相关提示语: 1 Attempting to schedule a local notification2 with an alert but haven ...

  4. CSS learnning...

    "Whenever this property changes, apply that change slowly." The property transition: width ...

  5. NGUI Button 3中点击事件的触发

    NGUI事件的种类很多,比如点击.双击.拖动.滑动等等,他们处理事件的原理几乎万全一样,本文只用按钮来举例. 1.直接监听事件 把下面脚本直接绑定在按钮上,点击按钮触发的方法名必须为OnClick,当 ...

  6. AC自动机妙用

    理解题意之后,很自然的想到了用AC自动机搞,结果网上一搜,全是暴搜,按照自己的思想,AC自动机搞起,果然在提交了数次之后,看到了Accept. AC自动机需要三个步骤: 第一步:建立字典树: 第二步: ...

  7. python第一步

    安装2.7的python 环境:到cmd下python,就可以跑代码了,要是想运行py文件,在命令行python test.py,记得在windows下把python加入环境变量 学习基础的语法: 注 ...

  8. 帝国cms7.0调用指定栏目,指定顺序排列

    [e:loop={"select * from {$dbtbpre}enewsclass where classid in (82,83,86,87,88,89,90,91,93) orde ...

  9. 帝国cms修改[!--show.listpage--]分页页码所生成的html标签

    在使用帝国cms系统时,我们用[!--show.page--]和[!--show.listpage--]来生成页码 其中[!--show.listpage--]所生成的html页码代码为: <a ...

  10. delphi 文件夹权限设置(执行一个小脚本的笨办法)

    如题,研究了一天,也没再网上找到比较好的方式,自己做了一个.方法如下: 1.创建一个 cmd 命令文件.2.调用该命令. 代码如下:   S:='echo y|cacls h: /t /c /g ev ...