日期:2014-05-17  浏览次数:20741 次

请教一个关于窗口函数的问题
请教一个关于窗口函数的问题:

H_DATE          编码    名称     数量  累计数量   金额    累计金额
2013-01-04 8000013  A 100 100 1000 1000
2013-01-05 8000013  A 100 200 1000 2000
2013-01-07 8000013  A -200 0 -1990 10
2013-01-04 8000013  B 500 500 2000 2010
2013-01-06 8000013  B -500 0 -1550 460

使用下面的函数计算累计值,把当日+后一日的数量和金额相加放在累计字段上,负号为卖出
nvl(sum(数量) over (order by 名称,h_date rows unbounded preceding),0) as ljsl,
nvl(sum(金额) over (order by 名称,h_date rows unbounded preceding),0) as ljje

处理数量的时候累加后为0,但金额会有一个余额,在累加金额的时候,B会把A剩余的10也加入到B里面,如何按照名称不把A剩余的10加到B里面去?

谢谢!

------解决方案--------------------
nvl(sum(数量) over (partition by 名称 order by h_date rows unbounded preceding),0) as ljsl,
nvl(sum(金额) over (partition by 名称 order by h_date rows unbounded preceding),0) as ljje