Tax calculation not correct
I have an error in this query, can someone please tell me what I'm doing wrong? This query works until I try and discount something at 100%. At 100%, I'm getting a $10.99 tax on an $11.25 item. The tax should show 0.00.
(tax_state + tax_fed)/100*(SUM( t3.price * t1.quantity)*SUM( t3.price * t1.quantity)-(SUM(t1.discount) + t3.disc开发者_StackOverflowount)/100)
EDIT:
This works now and gives me the correct tax but is a negative number. It shouldn't be negative.
(SUM(price*quantity)*(SUM(discount)+t3.discount)/100-SUM(price*quantity))*(tax_state+tax_fed)/100 AS tax
You're calculating the discount wrong. your math boils down to:
taxes * ( (cost * cost) - discount)
which means you're squaring the cost, subtracting a small discount value, and multiple by taxes. Most likely you'd want something more like
taxes * (cost - (cost * discount))
or, doing some algebraic extraction
taxes * cost * (1 - discount)
instead, which'd be
(tax_state + tax_fed)/100 * SUM(t3.price * t1.quantity) * (1 - (SUM(t1.discount) + t3.discount) / 100)
精彩评论