Grouping and getting Variance from 2 tables for selected Items

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi All, I'm trying to group and get a Variance from 2 tables for selected Items.
I have the sample table formats and expected result table format below.
Please help me out.

Regards,
Pedie

Sheet4
BCDEFGHIJKLM
1table-1table-2expected result
2IDINV-TCKAmtIDINV-TCKAmtINV-TCKAmt-Tb1Amt-Tb2Var
31SD-HHH-JK-120921SD-HHH-JK-16138SD-HHH-JK-1613811333-5195
42SD-HHH-JK-120442SD-HHH-JK-23878SD-HHH-JK-2387818162-14284
53SD-HHH-JK-171973SD-HHH-JK-34840SD-HHH-JK-3484015948-11108
628SD-HHH-JK-272284SD-HHH-JK-45981SD-HHH-JK-4598116207-10226
729SD-HHH-JK-271405SD-HHH-JK-57509SD-HHH-JK-575097748-239
830SD-HHH-JK-237946SD-HHH-JK-64288SD-HHH-JK-6478815231-10443
931SD-HHH-JK-374436SD-HHH-JK-6500
1032SD-HHH-JK-36210
1133SD-HHH-JK-32295
1234SD-HHH-JK-47083
1335SD-HHH-JK-45957
1436SD-HHH-JK-43167
1537SD-HHH-JK-52497
1638SD-HHH-JK-51930
1739SD-HHH-JK-53321
1840SD-HHH-JK-63052
1941SD-HHH-JK-65145
2042SD-HHH-JK-67034

<thead>
</thead><tbody>
</tbody>









<tbody>
</tbody>
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
i tried this but sure didnt work out. table 2 items are getting duplicate amt
SELECT tb1.ID, tb1.[INV-TCK], Sum(tb1.AMT) AS SumOfAMT, Sum(tb2.Amt) AS SumOfAmt1, Sum(([tb2].Amt-[tb1].Amt)) AS VAR
FROM tb2 INNER JOIN tb1 ON tb2.[INV-TCK] = tb1.[INV-TCK]
GROUP BY tb1.ID, tb1.[INV-TCK];
 
Upvote 0
I created 2 queries by grouping them as INV field level then created another query (which is the 3rd one) to combine both of them. Now it seems to give me the correct result.
Please validate it for me.

Code:
[/FONT][/SIZE][SIZE=4][FONT=courier new]SELECT Qry2.[INV-TCK], [Qry2].[SumOfAmt]-[Qry1].[SumOfAmt] AS Var[/FONT][/SIZE]
[SIZE=4][FONT=courier new]FROM ([/FONT][/SIZE]
[SIZE=4][FONT=courier new]SELECT tb1.[INV-TCK], Sum(tb1.AMT) AS SumOfAMT FROM tb1 GROUP BY tb1.[INV-TCK])  AS Qry1[/FONT][/SIZE]
[SIZE=4][FONT=courier new] INNER JOIN ([/FONT][/SIZE]
[SIZE=4][FONT=courier new]SELECT tb2.[INV-TCK], Sum(tb2.Amt) AS SumOfAmt, tb2.X1, tb2.[X2] FROM tb2 GROUP BY tb2.[INV-TCK], tb2.X1, tb2.[X2] HAVING (((tb2.X1)='Y') And ((tb2.[X2])='Y')))  AS Qry2 [/FONT][/SIZE]
[SIZE=4][FONT=courier new]ON Qry1.[INV-TCK] = Qry2.[INV-TCK];[/FONT][/SIZE]
[SIZE=4][FONT=courier new]
 
Upvote 0

Forum statistics

Threads
1,203,483
Messages
6,055,663
Members
444,806
Latest member
tofanexcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top