How to SUM and SUBTRACT using SQL?

I think this is what you’re looking for. NEW_BAL is the sum of QTYs subtracted from the balance: If you want to update the item balance with the new balance, use the following: This assumes you posted the subtraction backward; it subtracts the quantities in the order from the balance, which makes the most sense … Read more

must appear in the GROUP BY clause or be used in an aggregate function

Yes, this is a common aggregation problem. Before SQL3 (1999), the selected fields must appear in the GROUP BY clause[*]. To workaround this issue, you must calculate the aggregate in a sub-query and then join it with itself to get the additional columns you’d need to show: But you may also use window functions, which looks simpler: The … Read more