Create Query to combine data

esbencito

New Member
Joined
May 22, 2017
Messages
16
Hi all,

I'm trying to combine two different data sources. Unfortunately, since I'm quite new to Access, I don't seem to be able to figure it out. I have the following two tables that I need to combine:

Raw Data Table:

Store IDStore NameStore TypeRetail ChannelSLS RTL
30001NANNING MIXCFSSRLS2500
30001NANNING MIXCFSSRLS1000
30001NANNING MIXCFSSRLS500
60002GRAND GATEWAYSISRLC250
60002GRAND GATEWAYSISRLC4500
60002GRAND GATEWAYSISRLC3250
60002GRAND GATEWAYSISRLC150
80003HANGZHOU XIAOSHANFSSRLE675
80003HANGZHOU XIAOSHANFSSRLE550

<tbody>
</tbody>

LookUp Table:

Store IDProvin
30001Guangxi
80003Zhejiang

<tbody>
</tbody>

Now I want to create a Query from both tables, matching the correct Province for each Store ID in my Raw Data table and fill a blank or zero if there's no match. So the result should be something like this:

Store IDStore NameStore TypeRetail ChannelProvinceSLS RTL
30001NANNING MIXCFSSRLSGuangxi2500
30001NANNING MIXCFSSRLSGuangxi1000
30001NANNING MIXCFSSRLSGuangxi500
60002GRAND GATEWAYSISRLC250
60002GRAND GATEWAYSISRLC4500
60002GRAND GATEWAYSISRLC3250
60002GRAND GATEWAYSISRLC150
80003HANGZHOU XIAOSHANFSSRLEZhejiang675
80003HANGZHOU XIAOSHANFSSRLEZhejiang550

<tbody>
</tbody>

How can I achieve that? I tried with below, but that doesn't seem to work...

Code:
SELECT D.Store ID, D.Store Name, D.Store Type, D.Retail Channel, L.Province, D.SLS RTLFROM Raw Data AS D, LookUp AS L
INNER JOIN LookUp
ON D.Store ID=L.Store ID
 

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.
If you have spaces in table or field names, you need to surround them in square brackets (best practice says to NOT use blank spaces in object or field names, but use something like underscores instead).
So query would need to look something like:
Code:
SELECT D.[Store ID], D.[Store Name], D.[Store Type], D.[Retail Channel], L.Province, D.[SLS RTL] 
FROM [Raw Data] AS D
INNER JOIN LookUp AS L
ON D.[Store ID]=L.[Store ID];
 
Upvote 0
Just tried it with your code, but I still get a "Type mismatch in expression" error? Unfortunately, it doesn't seem to locate the error more specifically...
 
Upvote 0
Sound like one of the STORE ID fileds is text and the other is a number - the best way to sort it is to change them both to be the same. one may be set to Number or AutoNumber - the other should just be number.

Test it like this and see if it works:


Code:
SELECT D.[Store ID], D.[Store Name], D.[Store Type], D.[Retail Channel], L.Province, D.[SLS RTL] 
FROM [Raw Data] AS D
INNER JOIN LookUp AS L
ON D.[Store ID][COLOR=#FF0000]*1[/COLOR]=L.[Store ID][COLOR=#FF0000]*1[/COLOR];


This should run - but you wont be able to edit the query in design view - therefore I would recommend always using the same datatypes.
 
Upvote 0
You're absolutely right! one field is stored as text the other one as number! Thanks!!

Though, your solution doesn't seem to work and I also cannot change the text field to a number field without losing data? When I import the data I chose "Integer" and it returns a list of "ImportErrors" and I'm only left with the actual numbers. The problem is, some of the Store IDs are e.g. "Store 1" some are e.g. "3001" and since I'm not handling the original database, I cannot change that... :rolleyes:
 
Upvote 0
you Should then create a subquery and use that to join - selecting only the numeric values (as that is the only ones that could match anyway).

Code:
SELECT D.[Store ID], D.[Store Name], D.[Store Type], D.[Retail Channel], L.Province, D.[SLS RTL] 
FROM [Raw Data] AS D
INNER JOIN LookUp AS L
ON D.[Store ID][COLOR=#ff0000]&""[/COLOR]=L.[Store ID][COLOR=#ff0000]&""[/COLOR];

Does this work?
 
Upvote 0
Yes!! That works! even though I'm not sure it always will, as the data might have text values as well at some point (currently it does not).

I just noticed, the query only returns the rows where it finds a match, and leaves out the other ones. How can I include everything and just keep Province blank for each store that doesn't have an assigned province?? The store still has sales data, so it cannot be left out...
 
Upvote 0
You should definitely try to get the datatype to be text or numeric on both.

That will make it much easier if you make changes etc.

Code:
SELECT D.[Store ID], D.[Store Name], D.[Store Type], D.[Retail Channel], L.Province, D.[SLS RTL]
FROM [Raw Data] AS D LEFT JOIN LookUp AS L ON D.[Store ID]&"" = L.[Store ID]&"";

This will work if the join fields have text in them, however you cant edit in design mode and you may find it slow with a large number of records.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

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