https://blog.csdn.net/sinat_26811377/article/details/107188400 参考
------------
已SqlServer为例
with t as(
select '1' [序号],'A' [料号],50 [数量],1200 [库存]
union ALL
select '2' [序号],'A' [料号],60 [数量],1200 [库存]
union ALL
select '3' [序号],'A' [料号],60 [数量],1200 [库存]
)
SELECT T2.序号,T2.料号,T2.数量,T2.库存+T2.LAG_ [KC] FROM (
SELECT *,LAG([SUM_],1,0) OVER(PARTITION BY [料号] ORDER BY 序号 ASC) [LAG_] FROM (
SELECT *,SUM(数量*(-1)) over(partition by 料号 ORDER BY 序号 ASC) [SUM_] FROM T
) T1
) T2