Click here to Skip to main content
15,850,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I need the output mentioned below

Table 1

ID 	Req NO		Req Qty

123	R123		10
123	R123		5
123	R124		20

Table 2

ID	Issue No	Issue Qty

123	R123		5
123	R123		5
123	R124		10

Output Should be

ID 	Req No 		Req Qty		Issue Qty
123	R123		15		10
123	R124		20		10

What I have tried:

SELECT DISTINCT tb_Structure_FMR.PC_Mark_ID, tb_Structure_FMR.FMR_RequestNo,tb_Structure_FMR.FMR_Request_Date,
SUM(CAST(tb_Structure_FMR.FMR_Request_Qty AS FLOAT)),
SUM(CAST(tb_Structure_MaterialIssuance.FMI_IssuedQty AS FLOAT)) FROM tb_Structure_FMR INNER JOIN
tb_Structure_MaterialIssuance ON tb_Structure_FMR.PC_Mark_ID = tb_Structure_MaterialIssuance.PC_Mark_ID
GROUP BY tb_Structure_FMR.PC_Mark_ID,tb_Structure_FMR.FMR_RequestNo,tb_Structure_FMR.FMR_Request_Date
Updated 13-Jun-21 0:52am

1 solution

You need to GROUP them first, then JOIN them:
SELECT t1.ID, t1.ReqNo, t1.TRQ, t2.TIQ
          FROM Table1 
        GROUP BY ID, ReqNo) t1
  JOIN (SELECT ID, IssueNo, SUM(IssueQty) AS TIQ 
          FROM Table2 
        GROUP BY ID, IssueNo) t2
    ON t1.ID = t2.ID AND t1.ReqNo = t2.IssueNo
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