0

i have a mysql table that contains services for invoices.

it looks like

servicename|ammount|price|tag(type of service)|discount(service worthy of discount, true or false)|clientDiscount(client worthy of discount, true or false)

I need to make a query that groups by year, sums(amount * price)

But, if discount and clientDiscount are true i need to count for a discount of 15%

I have tried with case and if statements but nothing works. I don't really know witch end to start in. And to be honest i can't really grasp how case and if statements work. I think i need to make one Sum that contains services with discount true and client discount true, then another sum that includes the services without discounts and add these upp to get a correct sum? Right now its like an ant farm in my head.

1
  • Provide some sample data and expected output. Commented Apr 24, 2015 at 6:56

2 Answers 2

1

You want this:

select year(invoicedate), sum(amount * price * if(discount and clientdiscount, 0.85, 1)) total
  from invoices
  group by year(invoicedate);

demo here

The if statement works like this. if(condition, value-if-true, value-if-false). So in your example, if discount and clientdiscount is true (ie, they are both 1), it will return your discount amount of 15% (a discount of X is the same as multiplying by 1 - X). If the condition is false, it will return 1, which won't modify the result of amount * price. In summary, after the if is processed, the sum becomes either:
sum(amount * price * 0.85) -- if discount and client discount are both 1
OR
sum(amount * price * 1) -- if discount and client discount are both 0

Sign up to request clarification or add additional context in comments.

3 Comments

Probably a dum question, but the values in my discount, clientDiscount columns are true and false not 1 or 0. Its easy to change though. But can it work as is with true or false value?
I LOVE YOU!!!!!!! Im almost there. Ive made two fields "as total" and " as totaldiscount", is it possible to make a sum of these two like sum(total - totaldiscount) as final? Here is a link to where i am now. sqlfiddle.com/#!9/ee57c/2/0 I get an error saying there is now total.
you can't refer to field aliases in the select clause, you would need to expand total and totaldiscount to the formula used to create them. sqlfiddle.com/#!9/ee57c/5
0

You could aggregate it without if. discount and clientdiscount are integers, so you may simply multiply it with your discount percent. Slightly modified script of @pala:

select year(invoicedate), sum(amount * price * (1 - discount * clientdiscount * 0.15)) total
  from invoices
  group by year(invoicedate);

2 Comments

due to associativity, 1 - discount * clientdiscount * 0.15 becomes 1-0 if either discount or clientdiscount are 0. and 1 - 0.15 if they are both 1.
@Jesper It's simple arithmetics. Value * (1 - 0.15) = Value * 0.85. discount and clientdiscount may be equal to 0 or 1. When both are equal to 1 you have to apply a discount. When one (or both) is 0, you have not to apply the discount. So, in the first case you will get Value * (1 - 1 * 1 * 0.15) = Value * 0.85. In the second case - Value * (1 - 0 * 0.15) = Value * 1 = Value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.