sql subtract

Solutions on MaxInterview for sql subtract by the best coders in the world

showing results for - "sql subtract"
Anaïs
06 Mar 2020
1I think this is what you're looking for. NEW_BAL is the sum of QTYs subtracted from the balance:
2
3SELECT   master_table.ORDERNO,
4         master_table.ITEM,
5         SUM(master_table.QTY),
6         stock_bal.BAL_QTY,
7         (stock_bal.BAL_QTY - SUM(master_table.QTY)) AS NEW_BAL
8FROM     master_table INNER JOIN
9         stock_bal ON master_bal.ITEM = stock_bal.ITEM
10GROUP BY master_table.ORDERNO,
11         master_table.ITEM
12If you want to update the item balance with the new balance, use the following:
13
14UPDATE stock_bal
15SET    BAL_QTY = BAL_QTY - (SELECT   SUM(QTY)
16                            FROM     master_table
17                            GROUP BY master_table.ORDERNO,
18                                     master_table.ITEM)
19This assumes you posted the subtraction backward; it subtracts the quantities in the order from the balance, which makes the most sense without knowing more about your tables. Just swap those two to change it if I was wrong:
20
21(SUM(master_table.QTY) - stock_bal.BAL_QTY) AS NEW_BAL
queries leading to this page
substract in sqlsql subtractsql subtract