减去oracle中前几个月的值

时间:2013-06-20 17:42:30

标签: oracle

我有一张如下表 -

MID Period  Total Amount    New Total Amount 
123 01  45566          Total Amount ie 45566
123 02  -5677          Total Amount – (Total Amount for month of 01) 
123 03  7899           Total Amount – (Total Amount for month of 01 
                               + TotalAmount for month of 02 )  
235 01  5677           Total Amount  i.e. 5677
235 02  768878         Total Amount –
                               (Total Amount for month of 01) ie 768878–(5677)
235 03  -676787 

如何编写查询以获取列新总金额。

谢谢!

1 个答案:

答案 0 :(得分:0)

total_amount - nvl(sum(total_amount) over 
    (partition by mid order by period 
     rows between unbounded preceding and 1 preceding), 0)