I have a table shipments:
--------------------------
id | event | quantity |
--------------------------
1 | received | 20 |
2 | sent | 30 |
3 | received | 45 |
I want a query that will sum all the received quantities, and subtract that value from the sent quantities. The query should return a result of 35.
SELECT SUM(quantity)
FROM shipments
WHERE event = 'received';
This query returns 65. How can I get it to also subtract the sent quantity (and get a result of 35)?