I have a query which produces a list of orders like this:
Invoice Description Qty Amount VAT VAT code
3761 Flyers -1 -100 0 Z
3761 Business Cards -1 -75 -11.25 S
3761 Business Cards -1 -75 -11.25 S
3761 Discount(10%) -1 25 0 Z
3761 Delivery -1 -27 -4.05 S
Essentially the VAT column is the Qty * Amount * 0.15, if the VAT Code is 'S' (I did this using an IF statement), otherwise 0.
This works fine for the orders apart from where there is a Discount row for that Invoice number (in this case Invoice 3761 has a Discount row). When there is a Discount row I need to work out the VAT differently (((Qty * (Amount - 10%)) * 0.15).
Is this even possible within MySQL to 'see' if there is a Discount row for the common Invoice number and change the VAT fields of other rows accordingly? If so what function should I use and can I combine this with an IF statement to produce the two VAT calculating formulas?