Share via

Join 1 field of a table to 2 based on criteria

Anonymous
2020-08-18T16:43:27+00:00

Hi, I want to do something very complicated. I have a field in a table called location that has a field called Residence, That field is populated with either a 2 letter state code or 5 letter city code.

I have another table called Core Table where I have 2 fields called city and state. The city field is a 5 letter city and state field is 2 letter state.

My goal is to create a join between the tables so that when Residence has 2 digits, it joins to the state field of the residence table and when it has 5 letters, it joins to city field.

Can this be done? If so, please show me what I have to do. Thanks much.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2020-08-19T11:18:55+00:00

Firstly, let's deal with why your present design is so wrong.  Before turning to specifics it's important to understand that the database relational model is a formal model and governed by formal rules.  One of the most basic of these is the Guaranteed Access Rule, which is stated as follows:

Every datum (atomic value) in a relational database is guaranteed to be logically accessible by resorting to a combination of table name, primary key value and column name.

This is rule #2 in the twelve rules published by E F Codd in the paper "Is Your DBMS Really Relational?" in ComputerWorld on14 October 1985.  It was Codd who introduced the database relational model to the world in his seminal paper in 1970.

What this means of course is that each column in a table can contain values of one attribute only, and those values must be legitimate values of the single attribute type which the column represents.  In your case, however, the column in your Location table contains values of two different attributes, city and state, and consequently violates the Guaranteed Access Rule.  To see how what you are attempting should be represented let's turn to my DatabaseBasics demo.  In this you'll find the following tables:

Cities

CityID  City              RegionID

1          Stafford       1

5          Paris           3

6          Paris           4

7          Lichfield      1

8          Newcastle  1

9          Derby         5

10        Stratford     6

11        Monaco      7

Regions

RegionID         Region                  CountryID

1                      Staffordshire         1

3                      Texas                    2

4                      Île-de-France        3

5                      Derbyshire            1

6                      Warwickshire        1

7                      N/A                        4

8                      California               2

9                      Pays de la Loire    3

Countries

CountryID    Country

1                   United Kingdom

2                   United States

3                   France

4                   Monaco

If the three tables are joined in a query the following result table can be returned:

City                      Region                    Country

Stafford               Staffordshire           United Kingdom

Lichfield               Staffordshire           United Kingdom

Newcastle           Staffordshire           United Kingdom

Paris                   Texas                       United States

Paris                    Île-de-France          France

Derby                   Derbyshire              United Kingdom

Stratford               Warwickshire         United Kingdom

Monaco                N/A                          Monaco

For all but one of the rows the city relates to a region, and the region to a country.  In the case of Monaco, however, the country has no regions, so how do we represent the fact that the city of Monaco is in the country of Monaco?  The answer is by this row in the Regions table:

RegionID         Region              CountryID

7                      N/A                    4

As you can see the 'region' N/A is referenced in cities by the value 7 in the RegionID foreign key column, which references RegionID 7 of the primary key in the above row.  The N/A row is therefore plugging the gap in the discontinuous hierarchy which results from Monaco having no regions.

In the above model it is assumed that we know which region any city is in, so the N/A row is only necessary in the case of small countries like Monaco which have no regions.  However, we might want to be able represent which country a city is in if we don't know which region it is in.  To do this we would extend the Regions table as follows:

Regions

RegionID         Region                  CountryID

1                      Staffordshire         1

3                      Texas                    2

4                      Île-de-France        3

5                      Derbyshire            1

6                      Warwickshire        1

7                      N/A                        4

8                      California               2

9                      Pays de la Loire    3

10                    Unknown               1

11                    Unknown               2

12                    Unknown               3

We now have an Unknown region for every country which has a regional structure and one N/A region for the country that doesn't, so, if in the UK for instance we need to enter the city Wakefield, but do not know which region it is in we'd enter the following row in Cities:

CityID      City              RegionID

12            Wakefield    10

In the query which joins the tables this would return:

City               Region        Country

Wakefield     Unknown     United Kingdom

Your situation is analogous.  The column in your Location table should reference only the city column in your Core table, and you should add rows with a value in the city column of 'Unknown' for each state.  This means of course that, if it contains city names, the city column cannot be a candidate key of the Core table because of the duplicated 'Unknown' values.  Even without those rows, however, a column of city names cannot be a key, because city names can legitimately be duplicated (think how many Springfields there are in the USA, or even Staffords, of which there are four, all deriving their name from the original Stafford where I live).  Cities must be identified by a distinct key value, which is usually a number, but can be any coding system you wish provided that the values are distinct.

Was this answer helpful?

0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2020-08-19T12:09:14+00:00

    Note that the above reply has been edited.  I'd inadvertently juxtaposed the names of the Location and Core tables.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2020-08-18T23:53:58+00:00

    Hi Ken, I downloaded that file. Can you please tell me where in the database it shows how to do what I listed above? Thanks.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2020-08-18T18:11:45+00:00

    The way to cater for a discontinuous hierarchy like this is to include a row in the Location table for each state with a city value such as 'N/A' or 'Unknown'. A foreign key column in the referencing Core table can then reference the 'N/A' row for the state in question where you do not wish to reference a specific city in the state.

    For an example take a look at DatabaseBasics.zip n my public databases folder at:

    https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169

    In this little demo file the section on 'entering data via a form/subforms' includes three correlated combo boxes to select a country, region, and city.  You'll note that in the case of Monaco the region is N/A because, being such a small country, there are no regional units.  The city is the entire country.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2020-08-18T17:20:11+00:00

    You have a fundamental data normalization and structure problem.

    You could create a union query and join that, but that will have a hit on performance ...

    You would be much better off setting up your tables so this isn't an issue.  Have a PK value in your Core table and in your Residence have 2 combo boxes the allow them to enter by city or state.

    Better yet, have 2 distinct fields of that is truly required.

    The issue here is we don't know enough about your data, forms to guide you properly.  If you explain further, maybe post images, copy of your db, we can try to help more.

    Was this answer helpful?

    0 comments No comments