Combine/Join 2 tables and get an output in 3rd table

sunilbsrv2k

Board Regular
Joined
May 25, 2018
Messages
73
hi all,

I have 2 different tables in Excel; I have managed to get them into Access database in Table format.

But I have a task to join these 2 table and get a new table.

I have written the query, but its not working; while executing I get the error that its too complex....

Request your help. Please find below the query I have written:

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]SELECT ([First Name]+' '+[Last Name]) AS [Employee Name], [Person Number], [Work Email], [Termination Date], [Last Day Worked], [Legal Employer Hire Date], [Termination Action], [Termination Reason], [Assignment Number], [Primary Assignment Flag], [Assignment Status], [Assignment Category], [Person Type], [Business Unit], [Department], [Job Code], [Job], [Local Job Title], A.[Location], A.[Cost Center], A.[Cost Center Description ], [Grade], A.[Manager], [Manager Person Number], [Manager Email], [Manager Type], MID(A.[Cost Center],4,8) AS [Cost Center Number], B.[SBU Name], B.Manager AS [SBU Leader], B.LOB, B.Location AS Site
FROM AKS_DP_Headcount_Master_DLY AS A LEFT JOIN AKS_DP_Cost_Center_Master AS B ON A.[Cost Center Number]=B.[Cost Center];

Thanks in advance.

Regards
sunil[/FONT]
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
How big are these tables (how many record each)?
What is the relationship between these two tables (One-to-one, one-to-many, many-to-many)?

Are there any blanks or errors on the two fields you are joining ("Cost Center Number" and "Cost Center")?
Are these two joining fields both the same Data Type (i.e. both Text or both Numeric)?

I find that the best way to debug issues like this is to start small, and work your way up, i.e. does the following work?
Code:
SELECT A.[Cost Center Number] 
FROM AKS_DP_Headcount_Master_DLY AS A 
INNER JOIN AKS_DP_Cost_Center_Master AS B 
ON A.[Cost Center Number]=B.[Cost Center];
If so, then change the join type of LEFT JOIN, and see if that works.
If it does, try adding your other fields one-by-one, until you encounter the error (and then you will know where to focus your attention).

I would also recommend prefacing EVERY field in your SELECT clause with the alias that it is coming from (A or B).
 
Last edited:
Upvote 0
Hi Joe
Thanks for your response.

I tried applying table names A or B to the respective fields; it worked...

However, not sure why it popped up asking me to enter a Parameter for A.Cost Center Description and again for A. Cost Center Number. The values entered reflected in the entire column...

Further, this query gave results with columns from B table were completely blank..

Could you please advise...
Thanks
 
Upvote 0
However, not sure why it popped up asking me to enter a Parameter for A.Cost Center Description and again for A. Cost Center Number. The values entered reflected in the entire column...
If it asks you for parameters, then it means that you have a typo. It means it cannot find a field by that name. So either you:
1. Included a field that does not exist
2. Made a type
3. Attributed the field to the wrong table (A or B)

Further, this query gave results with columns from B table were completely blank..
You need to decide what kind of join that you want.
- An Inner Join will only return records where you having matching records in both tables
- A Left Outer Join will return ALL records from Table A, and all the matches from Table B. So if there are some records in Table A with no matching value in Table B, any Table B fields for those record will show as Null/Empty/Blank in your query. You can use the NZ function if you like to return so default value, in those spaces, if you like.

Also make sure that the fields that you are joining on do not have any errors or blanks in them in the underlying tables.
 
Upvote 0
Hi Joe,

Thanks for your advice.

I checked the query again and found that there is a mismatch in the type of data in the field named "Cost Center".

The column in table A contains IN-12345678-101 but Table B contains 12345678.

If you could look at my query above, I am extracting 12345678 as well...

But not sure how to use it in Joining A and B

Could you please help.

Thanks
 
Upvote 0
Is "Cost Center" in Table B a Text/String or Numeric field?
If numeric, try this:
Remove the "ON" clause from the query, and instead add:
Code:
WHERE [COLOR=#333333]MID(A.[Cost Center],4,8)=B.[Cost Center][/COLOR]
if "Cost Center" in table B is text, or
Code:
WHERE [COLOR=#333333]MID(A.[Cost Center],4,8)=LEFT(B.[Cost Center],8)[/COLOR]
if "Cost Center" in table B is numeric.
 
Upvote 0
Hi Joe,

Thanks much.

I tried both the syntaxes. However, I am getting error as Syntax error in From Clause.

Below is the updated query giving me error:

Code:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]SELECT (A.[First Name]+' '+A.[Last Name]) AS [Employee Name], A.[Person Number], A.[Work Email], A.[Termination Date], A.[Last Day Worked], A.[Legal Employer Hire Date], A.[Termination Action], A.[Termination Reason], A.[Assignment Number], A.[Primary Assignment Flag], A.[Assignment Status], A.[Assignment Category], A.[Person Type], A.[Business Unit], A.[Department], A.[Job Code], A.[Job], A.[Local Job Title], A.[Location], A.[Cost Center], A.[Cost Center Name], A.[Grade], A.[Manager], A.[Manager Person Number], A.[Manager Email], A.[Manager Type], MID(A.[Cost Center],4,8) AS CCnum, B.[SBU Name], B.Manager AS [SBU Leader], B.LOB, B.Location AS Site
FROM AKS_DP_Headcount_Master_DLY AS A LEFT JOIN AKS_DP_Cost_Center_Master AS B WHERE MID(A.[Cost Center],4,8)=LEFT(B.[Cost Center],8);[/FONT]
 
Upvote 0
Sorry, we need to remove the JOIN part too, and add the other table to the FROM clause, i.e.
Code:
[FONT=Verdana]SELECT (A.[First Name]+' '+A.[Last Name]) AS [Employee Name], A.[Person Number], A.[Work Email], A.[Termination Date], A.[Last Day Worked], A.[Legal Employer Hire Date], A.[Termination Action], A.[Termination Reason], A.[Assignment Number], A.[Primary Assignment Flag], A.[Assignment Status], A.[Assignment Category], A.[Person Type], A.[Business Unit], A.[Department], A.[Job Code], A.[Job], A.[Local Job Title], A.[Location], A.[Cost Center], A.[Cost Center Name], A.[Grade], A.[Manager], A.[Manager Person Number], A.[Manager Email], A.[Manager Type], MID(A.[Cost Center],4,8) AS CCnum, B.[SBU Name], B.Manager AS [SBU Leader], B.LOB, B.Location AS Site
FROM AKS_DP_Headcount_Master_DLY AS A, AKS_DP_Cost_Center_Master AS B 
WHERE MID(A.[Cost Center],4,8)=LEFT(B.[Cost Center],8);[/FONT]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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