0

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

1
  • Your current query look fine to me, it should work as it is. Now how complex your actual query is? without looking at the actual query nothing can be said. Commented Mar 5, 2014 at 23:07

3 Answers 3

1

Try this

select ProductID, (inv.Inventory + s.Sales) as BalanceInventory
from
( 
    select  ProductID, Inventory
    from [table]
    where xxx
) inv
left outer join
(
   select  ProductID, Sales 
    from [table]
    where xxx
) s on (s.ProductID = inv.ProductID)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Thit Lwin Oo. This works with a little modification on the IsNull part, which was suggested by Thiago Cardoso. I used this code: select ProductID, (inv.Inventory + isnull(s.Sales,0)) as BalanceInventory from ( select ProductID, Inventory from [table] where xxx ) inv left outer join ( select ProductID, Sales from [table] where xxx ) s on (s.ProductID = inv.ProductID)
Just be aware that if there is one or more sales records but no inventory record for a product, this solution will ignore the product entirely. i.e. it won't include the sales at all.
Thanks ElectricLlama.. I figured that out.. But, in my case, the Sales records are always a sub-set of Inventory records.. Thanks for the caution though..
1

The other option is

SELECT UnionTable.ProductID, SUM(UnionTable.BalanceInventory) AS BalanceInventory
FROM (
    SELECT ProductID, Inventory As BalanceInventory
    FROM Table1
    UNION ALL
    SELECT ProductID, Sales As BalanceInventory
    FROM Table2
) As UnionTable
GROUP BY UnionTable.ProductID

To subtract instead of add, simply make one part negative:

SELECT UnionTable.ProductID, SUM(UnionTable.BalanceInventory) AS BalanceInventory
FROM (
    SELECT ProductID, Inventory As BalanceInventory
    FROM Table1
    UNION ALL
    SELECT ProductID, -Sales As BalanceInventory
    FROM Table2
) As UnionTable
GROUP BY UnionTable.ProductID

3 Comments

Thanks ElectricLlama. This solution also works just fine. I tried both the solutions and got the same result. Just one question, in case we have to subtract the two values, how do we replace the SUM part?
Use my sql script. just change (inv.Inventory - s.Sales)
Yes Thit Lwin Oo. I did that and got the results. Was just trying to know how to modify the query given by ElectricLlama to subtract values instead of adding them. Anyways, thanks a ton to all of you. I guess I should have posted this query an hour back. Could have saved some time. Cheers.!!
0

You can avoid the case expression using the "isNull(expr, 0)".

About your problem, if you can't use any JOIN in your query, try to use UNION operator: http://technet.microsoft.com/en-us//library/ms180026.aspx

1 Comment

In all my queries, i was using the case expression to avoid null values. I didn't know about IsNull. Thanks Thiago. This will help me a lot. Cheers.!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.