I have this query:
SELECT (
SELECT purchase_code
FROM qa_suppliers_invoices a
WHERE a.supplier_invoice_code = b.item_invoicecodesupplier
) AS purchase_code,
item_code,
status_code,
itempurchase_quantity,
item_costprice
FROM qa_items_purchases b
ORDER BY purchase_code LIMIT 0,20000;
It return the following data

I want to GROUP like a Purple color line using (item_code) row, i want to add all the (itempurchase_quantity) making reference to (status_code) row then apply this ecuation (status_15 – status_16) also like the image.
I want this result, GROUP():
purchase_code item_code status_code itempurchase_quantity item_costprice
1 1506 15 713 126.94
2 1503 16 12 0.00
2 1683 15 9 25.28
2 1931 15 60 0.00
In the case of item_code(1997) not exist status_code = 15 There is thus return a negative number(-9) then you do not need to show.
2 2325 16 50 849.97
NOTE:
I need to disappear all 16 status_code
Demo data to play with query at sqlfiddle
UPDATE:
This is a picture detailing what I need:

1 Answer