I have two Microsoft SQL queries both of which give two columns but different number of rows. These are the results:
First query:
ProductID, Inventory
1, 100
2, 50
3, 200
Second query:
ProductID, Sales
1, -20
2, -50
I want to get the below output:
ProductID, BalanceInventory
1, 80
2, 0
3, 200
I have tried using plus sign in the query like this:
Select t1.ProductID,
t1.Inventory + (Case when t2.Sales is null then 0 else t2.Sales end) as 'BalanceInventory'
from t1 full join t2 on t1.ProductID = t2.ProductID
The issue with this is that the DB structure is designed in such a way that Sales and Inventory cannot be run in the same query. So, I need to run two separate queries and then add the two columns Inventory and Sales for every ProductID.
The actual DB structure and query is much more complex. I have tried to simplify the problem by creating a hypothetical one.
Please help. This is eating up my head.
Thanks, Karan