oracle 月销售总金额的累加计算,实现 n = (n-1) + (n-2)

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-23 16:08   11   0

在Oracle 当中如要实现下面这种情况

2 月分的销售总金额= 1 月分金额 + 2 月分的金额

3 月分的销售总金额= 1 月分金额 + 2 月分的金额 + 3 月分的金额。

...

12 月分的销售总金额= 1 + 2 + ... 12 月分金额。

现在在有一张表如数据如下图所示。

现在用 下面代码实现当前月销售总金额累加的计算

select year, month,sum(amount) as month_sum,
sum(sum(amount))over(order by month rows between unbounded preceding and current row)cumulative_amount
from all_sales t group by year,month


主要是 sum(sum(amount))over(order by month rows between unbounded preceding and current row) 句话,按月销售金额进行相加。

第N值= n -1 的值 + n-2 的值。


如果想求第 6 个份 到10月份的话

select year, month,sum(amount) as month_sum,
sum(sum(amount))over(order by month rows between unbounded preceding and current row)cumulative_amount
from all_sales t 
where t.month between 6 and 10
group by year,month



分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP