Given the following table
ITEMID | TYPE | QTY |
-----------------------
...
134 |TOTALINDEPOT | 169 |
134 |UNUSED | 70 |
134 |FAULTY | 15 |
134 |DAMAGED | 1 |
134 |DELAYED | 100 |
...
What is an efficient way to retrieve the quantity of available items with id 134 (excluding these of delayed type)? Result is 83 => (169-(70+15+1)).
I implemented it with (SELECT qty FROM tableA WHERE type='TOTALINDEPOT' AND itemid='134') - (SELECT SUM(qty) ... WHERE TYPE IN ('UNUSED', 'FAULTY', 'DAMAGED')) AND itemid='134'
, but looking for something more elegant.