I have data in two tables as below
Table 1 :
Material |Ordr Qty
---------|---------
abcd |4253
Table 2 :
Material | Stck Qty
---------|---------
abcd |1000
abcd |2000
abcd |2000
Expected Output :
Material |Ordr Qty |Stck Qty |Column D
---------|---------|---------|---------
abcd |4253 |1000 |3253
abcd |4253 |2000 |1253
abcd |4253 |2000 |-747
Logic for column D is like
4253-1000 = 3253
3253-2000 = 1253
1253-2000 = -747
LAG(ordr qty - stck qty,1,0)over (ORDER BY material)-stck qty
I am trying above LAG function and getting below output
abcd 4253 1000 -1000
abcd 4253 2000 1253
abcd 4253 2000 253
Let me know how to achieve the expected output.