Well, I have a mind-scratcher that I cannot solve. Total newbie :) I need to calculate stock item quantity and detect negative values if they appear in calculation:
inquantity | outquantity
100 | 0
10 | 0
0 | 50
0 | 100
20 | 0
0 | 80
15 | 0
100 | 0
And I need to calculate Quty:
inquantity | outquantity | Quty
100 | 0 | 100
10 | 0 | 110
0 | 50 | 60
0 | 100 | -40
20 | 0 | -20
0 | 80 | -100
15 | 0 | -85
100 | 0 | 15
How can i do that ?
Regarding Abhik's post:
select
id ,
inquantity,
outquantity,
@qty:= (@qty+inquantity)-outquantity as qty
from quantity,(select @qty:= 0 )r
order by id;
is there a possibility to reset the variable @qty on productid change?
+----+-----------+------------+-------------+------+
| id | productid | inquantity | outquantity | qty |
+----+-----------+------------+-------------+------+
| 1 | 1 | 100 | 0 | 100 |
| 2 | 1 | 10 | 0 | 110 |
| 3 | 1 | 0 | 50 | 60 |
| 4 | 1 | 0 | 100 | -40 |
| 5 | 2 | 20 | 0 | 20 |
| 6 | 2 | 0 | 80 | -60 |
| 7 | 2 | 15 | 0 | -45 |
| 8 | 3 | 100 | 0 | 100 |
+----+-----------+------------+-------------+------+