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

//方法一:

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. Yii2简单纪要

    网上经常拿Yii来类比ROR,从MVC角度,使用体验及代码风格上确实有很多相似的地方.不过看配置文件发现Yii2不止是受rails的影响,同样有不少spring的影子,最明显的就是配置文件中很多IOC ...

  2. 第八届河南省赛G.Interference Signal(dp)

    G.Interference Signal Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 35  Solved: 17 [Submit][Status ...

  3. uva-12657 - Boxes in a Line(双向链表)

    12657 - Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to righ ...

  4. ICE

    一.Slice-to-C++映射 1.引言 其映射定义:怎样把Slice数据类型翻译成C++类型,客户怎样调用操作.传递参数.处理错误. C++映射完全是线程安全的.例如,类的引用机制针对并行访问机制 ...

  5. 2.Freshman阶段学习内容的确定

    我刷知乎.在知乎上答题的程序员,不是很牛逼就是更牛逼,说起各种系统.各种系统的各种版本.各种语言.数据库.算法.IT届的各种圣战都有板有眼.信手拈来.头头是道,不得不服.这导致了一些非常严重的问题:我 ...

  6. sql--关于exec和sp_execute

    sql:exec与sp_excutesql的比较 exec与sp_execute都可以执行存储过程和批处理动态sql语句,以下所属均是关于批处理动态sql语句方面. 一.关于输入参数与输出参数 1.使 ...

  7. iOS实践03

    主要目标:版本新特性界面,新浪授权界面(登录界面)的处理 任务基本完成了,基本的框架也就到这了,接下来的应该是首页获取微博了. 1.版本新特性,可以单独作为一个model,写完之加入到项目中.我们新建 ...

  8. HDU 5226 Tom and matrix(组合数学+Lucas定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5226 题意:给一个矩阵a,a[i][j] = C(i,j)(i>=j) or 0(i < ...

  9. BZOJ 3916: [Baltic2014]friends( hash )

    字符串哈希..然后枚举每一位+各种判断就行了 ----------------------------------------------------------------------------- ...

  10. .net 更改日期格式

    示例:更改日期格式 下面的代码示例使用 Regex.Replace 方法将 mm/dd/yy 格式的日期替换为 dd-mm-yy 格式的日期. static string MDYToDMY(strin ...