Nick
Nick

Reputation: 19

SQL reset variable value if id changes

SELECT * FROM quantity;
+----+-----------+------------+-------------+
| id | productid | inquantity | outquantity |
+----+-----------+------------+-------------+
|  1 |         1 |        100 |           0 |
|  2 |         1 |         10 |           0 |
|  3 |         1 |          0 |          50 |
|  4 |         1 |          0 |         100 |
|  5 |         2 |        200 |           0 |
|  6 |         2 |          0 |         200 |
|  7 |         2 |         15 |           0 |
|  8 |         3 |        100 |           0 |
|  9 |         3 |         50 |           0 |
| 10 |         3 |          0 |           5 |
+----+-----------+------------+-------------+

to calculate quantity for specific product i do this

SELECT 
id , productid,
inquantity,
outquantity,
@qty:= (@qty+inquantity)-outquantity as qty 
from quantity,(select @qty:= 0 )r 
WHERE productid=1;
+----+-----------+------------+-------------+-----+
| 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 |
+----+-----------+------------+-------------+-----+

Is it possible to calculate this for all productid's? How can I reset variable @qty to start from 0 if productid is different from the privious?

To get this:

+----+-----------+------------+-------------+-----+
| 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 |        200 |           0 | 200 |
|  6 |         2 |          0 |         200 |   0 |
|  7 |         2 |         15 |           0 |  15 |
|  8 |         3 |        100 |           0 | 100 |
|  9 |         3 |         50 |           0 | 150 |
| 10 |         3 |          0 |           5 | 145 |
+----+-----------+------------+-------------+-----+

Upvotes: 0

Views: 247

Answers (2)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44874

I think

| 7 | 2 | 15 | 0 | -15 |

the qty would be 15 not -15

And you may get the result as

select
id,
productid,
inquantity,
outquantity,
qty from (
 SELECT 
 id , 
 productid,
 inquantity,
 outquantity,
 @qty:= if(@prev_prod = productid, ((@qty+inquantity)-outquantity),(inquantity-outquantity)) as qty,
 @prev_prod:=  productid 
 from quantity,(select @qty:= 0,@prev_prod:=0 )r
 order by productid,id 
)x; 

Upvotes: 0

M Khalid Junaid
M Khalid Junaid

Reputation: 64496

you can use case statement and another variable to check for same product id

SELECT 
id , productid,
inquantity,
outquantity,
@qty := case when @g = productid then @qty else 0 end,
@qty:= (@qty+inquantity)-outquantity as qty ,
@g := productid
from quantity,
(select @qty:= 0 ,@g=null)r 
order by productid

DEMO

Upvotes: 1

Related Questions