Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all ,

I have three tables:
OITM (ItemCode,Name),
OITW (ItemCode,WareHouseCode,StockHand,OrderQty),
WITM (ItemCode,LastUStock)

Now I want to join the three tables and I want to select he values of ItemCode and sum(StockHand - OrderQty) grouped by ItemCode where the sum(StockHand - OrderQty) <> LastUStock. How to get this value using join query?


SQL
Select OITM.ItemCode,sum(StockHand - OrderQty) from OITM  inner join WITM on OITM.ItemCode= WITM.ItemCode inner join OITW on OITM.ItemCode =OITW.ItemCode and OITW.WareHouseCode in ('WH1','WH2','WH4')  Group by OITM.ItemCode Having sum(StockHand - OrderQty) <> WITM.LastUStock


please help any one

Thanks
Surajit
Posted
Updated 24-Oct-11 16:43pm
v2
Comments
AspDotNetDev 24-Oct-11 20:16pm    
Please format your question better and include sample data and sample output, with clear examples of what you are trying to do.
JOAT-MON 24-Oct-11 22:44pm    
Edit: added code block and modified for readability

1 solution

Try modifying the query. The following line should not be a part of the join structure itself, but rather a filter for the join structure:
SQL
OITW.WareHouseCode in ('WH1','WH2','WH4')

Put it in as a WHERE clause:
SQL
SELECT
   OITM.ItemCode,
   sum(StockHand - OrderQty) AS NetStockHand
FROM
   OITM inner join 
   WITM on OITM.ItemCode = WITM.ItemCode inner join 
   OITW on OITM.ItemCode = OITW.ItemCode 
WHERE 
   OITW.WareHouseCode in ('WH1','WH2','WH4')
GROUP BY 
   OITM.ItemCode 
HAVING
   sum(StockHand - OrderQty) <> WITM.LastUStock
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900