I have two tables: Issue and Return. Both tables have columns article_id, person_id and quantity. I need to subtract Return table from Issue table for one person and group the result by article name.
First select looks like this:
SELECT Article.Name, SUM(Issue.Quantity)
FROM Issue LEFT JOIN Article ON (Issue.ArticleID = Article.ID)
WHERE Issue.PersonID = 2
GROUP BY Article.Name
ArticleName | Quantity ------------------------ Shoes | 5 Coats | 3 Hats | 3
Second like this:
SELECT Article.Name, SUM(Return.Quantity)
FROM Return LEFT JOIN Article ON (Return.ArticleID = Article.ID)
WHERE Return.PersonID = 2
GROUP BY Article.Name
ArticleName | Quantity ------------------------ Shoes | 3 Coats | 2 Hats | 0
Question is, how do I subtract second select from the first (Return – Issue) to get a table like this:
ArticleName | Quantity ------------------------ Shoes | 2 Coats | 1 Hats | 3
The Quantity in the final result should be the number of remaining articles to be returned.
This may work:
(heavily edited – my first answer did not have a chance of working…)