Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two table

PR_DET(IT_Code,ReqQty,ReqDate, IT_Desc)

PO_DET(IT_Code,IT_Status, PO_Qty)



I want to display

IT_Code, ReqQty, ReqDate, IT_Desc from PR_DET

and want add one more column Qty_Underpurchase
showing sum(PO_Qty) where PO_DET.IT_Status = 'UnderPurchase'

how can i achive this please help me out?
Posted
Comments
agent_kruger 20-Nov-13 6:47am    
is there something that joins both the tables like a link?
Manzoor Ahmed P 20-Nov-13 6:48am    
IT_Code is the link between two

1 solution

Try something like this:
SQL
SELECT pr.IT_Code, pr.ReqQty, pr.ReqDate, pr.IT_Desc, SUM(po.PO_Qty) AS Qty_Underpurchase FROM PR_DET AS pr
INNER JOIN PO_DET AS po ON po.IT_Code = pr.IT_Code
WHERE po.IT_Status = 'UnderPurchase'
GROUP BY pr.IT_Code, pr.ReqQty, pr.ReqDate, pr.IT_Desc


For Joins: http://technet.microsoft.com/en-us/library/aa213233(v=sql.80).aspx[^]

If you use an aggregate function (like SUM is one) all other columns you want to show need to be either an aggregate function too or be in the GROUP BY clause.

Edit: Fixed the SQL (was missing a ")" ;) )
 
Share this answer
 
v2

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