I have two tables:
-- Foo -- -- Bar --
Street | City | Sales Street | City | Purchases
X | A | 2 Y | B | 1
Y | B | 3 Z | C | 4
I have to return this:
Street | City | Profit
1 | A | 2
2 | B | 2
3 | C | -4
The only thing that came to my mind was this:
SELECT f.street, f.city, b.street, b.city, ISNULL(f.sales,0) - ISNULL(b.purchases,0) as Profit
FROM Foo f FULL JOIN Bar b
ON f.street = b.street AND f.city = b.city
I know this isn't what I need but got this result:
Street | City | Street | City | Profit
1 | A | NULL | NULL | 2
2 | B | 2 | B | 2
NULL | NULL | 3 | C | -4
How can I combine the columns so when it finds in one table use that one and vice versa?