Oracle 累計求和

來源:互聯網
上載者:User

//將當前行某列的值與前面所有行的此列值相加,即累計求和:  
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:視窗的終點是當前行,預設值,可以省略; 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.