窗口函數: 累加求和


https://blog.csdn.net/weixin_43332500/article/details/105033468

1、語法:

sum(字段1 ) over (partition by 字段2 order by 字段3 rows between unbounded preceding and current row) as 新字段名

2、功能:

實現組內累加

3. 例子

select name,mon,tota_amount,
sum(tota_amount) over(partition by name order by mon rows between unbounded preceding and current row) as account
from
(select name,mon,sum(amount) as tota_amount
from sheet1
group by name,mon) as a

select name,mon,tota_amount,
sum(tota_amount) over(partition by name order by mon rows between unbounded preceding and current row) as account
from
(select name,mon,sum(amount) as tota_amount
from sheet1
group by name,mon) as a

4、函數說明
sum(tota_amount)的求和是針對后面over()窗口的求和,
over中partition by name order by mon 針對name這一組按照月份排序,
rows between unbounded preceding and current row 限定了行是按照在當前行不限定的往前處理,通俗就是處理當前以及之前的所有行的sum,即3月時sum(amount)求的時1、2、3月的和,2月時sum(amount)求的是1、2月的和。unbounded意思無限的 preceding在之前的,current row當前行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM