I want to select information from two unrelated SQL tables (formed by separate SQL queries) to give me one table with four columns: one as an identifier, one for values from the first table, one for values from the second table, and one for a calculation between them.
A simplified example is:
tblSold
Shop name | items sold
Shop A | 100
Shop B | 50
Shop C | 75
Shop D | 80
tblReturned
Shop name | Items returned
Shop A | 10
Shop B | 5
Shop C | 7
Shop D | 8
And i'm trying to get a table that looks like this
Shop name | items sold | items returned | net (sold - returned)
Shop A | 100 | 10 | 90
Shop B | 50 | 5 | 45
Shop C | 75 | 7 | 68
Shop D | 80 | 8 | 72
In the above case, tblSold comes from a query that looks like this:
SELECT global.shopname, COUNT(stock.sold)
FROM global
INNER JOIN stock ON global.id = stock.shop
GROUP BY shopname
And tblReturned comes from a query that looks like this:
SELECT global.shopname, COUNT(stock.returned)
FROM global
INNER JOIN stock ON global.id = stock.shop
WHERE stock.datereturned > 1.01.2010
GROUP BY shopname
From this previous question SQL: Two select statements in one query I got the following query:
(SELECT global.shopname, COUNT(stock.sold)
FROM global
INNER JOIN stock ON global.id = stock.shop
GROUP BY shopname)
UNION
(SELECT global.shopname, COUNT(stock.returned)
FROM global
INNER JOIN stock ON global.id = stock.shop
WHERE stock.datereturned > 1.01.2010
GROUP BY shopname)
Which got me a table with only two columns, and no way to make the calculation. Is there a way to join these in some manner to make it work?