您的位置:首页 > 数据库 > Oracle

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

2011-11-30 09:24 399 查看
在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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: