oracle 累计求和

[c-sharp] view plain copy
  1. //将当前行某列的值与前面所有行的此列值相加,即累计求和:   
  2. //方法一:   
  3. with t as(    
  4.      select 1 val from dual union all    
  5.      select 3 from dual union all    
  6.      select 5 from dual union all    
  7.      select 7 from dual union all    
  8.      select 9 from dual)    
  9. select val,    
  10.        sum(val)    
  11.        over (order by rownum rows between unbounded preceding and current row)    
  12.        sum_val    
  13. from t    
  14. group by rownum,val    
  15. order by rownum;    
  16.        VAL    SUM_VAL    
  17. ---------- ----------    
  18.          1          1    
  19.          3          4    
  20.          5          9    
  21.          7         16    
  22.          9         25    
  23. //解析:    
  24. //sum(val)计算累积和;    
  25. //order by rownum 按照伪列rownum对查询的记录排序;    
  26. //between unbounded preceding and current row:定义了窗口的起点和终点;    
  27. //unbounded preceding:窗口的起点包括读取到的所有行;    
  28. //current row:窗口的终点是当前行,默认值,可以省略;   
  29. //  
  30. //方法二:  
  31. with cte_1 as(    
  32.      select 1 val from dual union all    
  33.      select 3 from dual union all    
  34.      select 5 from dual union all    
  35.      select 7 from dual union all    
  36.      select 9 from dual  
  37.      )   
  38. ,cte_2 as(  
  39.     select rownum rn,val from cte_1  
  40.     )  
  41. select a.val , sum(b.val) sum_val  
  42. from cte_2 a , cte_2 b  
  43. where b.rn <= a.rn  
  44. group by a.val  
  45. /  
  46. //方法三:  
  47. //创建一个递归函数,求和  
  48. //f(n) = x + f(n-1)  
  49. create table t  
  50. as  
  51. select 1 id,1 val from dual union all  
  52. select 2,3 from dual union all  
  53. select 3,5 from dual union all  
  54. select 4,7 from dual union all  
  55. select 5,9 from dual  
  56. /  
  57. create or replace function fun_recursion(x in int)   
  58. return integer is  
  59.        n integer :=0;  
  60. begin  
  61.      select val into n   
  62.      from t  
  63.      where id=x;  
  64.      if x=1 then  
  65.         return n;  
  66.      else  
  67.          return n + fun_recursion(x-1);  
  68.      end if;  
  69.      exception  
  70.      when others then  
  71.           dbms_output.put_line(sqlerrm);  
  72. end fun_recursion;  
  73. /  
  74. select val,fun_recursion(id) sum_val from t;  
  75.        VAL    SUM_VAL  
  76. ---------- ----------  
  77.          1          1  
  78.          3          4  
  79.          5          9  
  80.          7         16  
  81.          9         25  
  82. //  

 

原帖:http://topic.csdn.net/u/20110503/00/f518c144-5ae4-4843-bf54-056159b6f073.html?3783

关于unbounded preceding,参考:

[1].Order by range unbounded preceding

http://www.java2s.com/Tutorial/Oracle/0320__Analytical-Functions/Orderbyrangeunboundedpreceding.htm

[2].Order by, range unbounded preceding

http://www.java2s.com/Code/Oracle/Select-Query/Orderbyrangeunboundedpreceding.htm


原文地址:http://blog.csdn.net/bobo12082119/article/details/6386142

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值