user697508
user697508

Reputation: 25

MYSQL query - multiple likes

Stuck on this query. Obviously what I have below is wrong but hopefully it makes what I'm trying to do clear.

SELECT branch, 
SUM(price * quantity) as "Office" FROM orders WHERE account LIKE "0%" 
and 
SUM(price * quantity) as Personal FROM orders WHERE account LIKE "1%" 
GROUP BY branch;

I'm sure I'm way off here.

Thanks

Upvotes: 0

Views: 59

Answers (1)

lc.
lc.

Reputation: 116448

Use a case inside each sum:

select branch,
    sum(case when account like '0%' then price * quantity else 0 end) as Office,
    sum(case when account like '1%' then price * quantity else 0 end) as Personal
from orders
group by branch

Upvotes: 1

Related Questions