查询Oracle表库存中的获取余额

时间:2016-06-29 06:24:25

标签: sql oracle11g

我在oracle数据表中有样本数据,即 STOKREWARD 表,我想在数量 IN 和数量 OUT 之间取得平衡。

enter image description here

我希望得到这样的价值 BALANCES

enter image description here

我尝试使用这样的查询,但结果不是我想要的

SELECT STOKREWARD.NO, STOKREWARD.DODATE, STOKREWARD.REWARDNAME, STOKREWARD.NOTES,
STOKREWARD.QTYIN, STOKREWARD.QTYOUT,
(STOKREWARD.QTYIN- STOKREWARD.QTYOUT) AS BALANCES
FROM STOKREWARD
ORDER BY STOKREWARD.NO ASC

此查询给出结果:

enter image description here

谁能帮帮我?感谢

1 个答案:

答案 0 :(得分:1)

使用SUM() OVER

SELECT NO, DODATE, CODE, REWARDNAME, NOTES, QTYIN, QTYOUT,
    SUM (QTYIN - QTYOUT) OVER (ORDER BY DODATE ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS BALANCES
FROM STOKREWARD