sum over函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sum over函数相关的知识,希望对你有一定的参考价值。

参考技术A sum over函数用于统计累计求和值

select name,mon,amount ,sum(amount) over (partition by name order by mon from(select name,mon,sum(amount) as amount from order group by name,mon )t 

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当前行。

oracle 分析函数 - 总结

一、统计方面:
Sum() Over ([Partition by ] [Order by ])

Sum() Over ([Partition by ] [Order by ]  Rows Between  Preceding And  Following)
       
Sum() Over ([Partition by ] [Order by ] Rows Between  Preceding And Current Row)

Sum() Over ([Partition by ] [Order by ]
Range Between Interval ‘‘ Day Preceding And Interval ‘‘ Day Following )

二、排列方面:
Rank() Over ([Partition by ] [Order by ] [Nulls First/Last])

Dense_rank() Over ([Patition by ] [Order by ] [Nulls First/Last])
   
Row_number() Over ([Partitionby ] [Order by ] [Nulls First/Last])
   
Ntile() Over ([Partition by ] [Order by ])

三、最大值/最小值查找方面:
Min()/Max() Keep (Dense_rank First/Last [Partition by ] [Order by ])

四、首记录/末记录查找方面:
First_value / Last_value(Sum() Over ([Patition by ] [Order by ] Rows Between  Preceding And  Following  ))

五、相邻记录之间比较方面:
Lag(Sum(), 1) Over([Patition by ] [Order by ])

 

以上是关于sum over函数的主要内容,如果未能解决你的问题,请参考以下文章

oracle累积求和分析函数sum over的使用

oracle 求和函数

Oracle-分析函数之sum(...) over(...)

根据参数 -Oracle 重新启动 sum(over) 函数

如何使“SUM(amount) over”窗口函数在 MySQL 5.7 中工作?

MySQL - SUM() OVER() 函数用法详解