Results 1 to 4 of 4
  1. #1
    Etownguy is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    2

    Join for multiple tables?

    Newbie here.
    I need to join multiple tables in Access 2010, but am having trouble doing so properly. I have tried multiple ways to doing so, but have failed miserably.

    An example that explains what I want to do:

    TableA
    Site Year


    1 2009
    1 2010
    2 2009
    2 2010
    3 2009
    3 2010
    4 2009
    4 2010


    TableB
    Site Year X1 X2
    1 2009 0.1 A
    1 2010 0.2 B
    3 2009 A

    TableC
    Site Year X3
    1 2010 1.1
    2 2009 2.2
    4 2009 1.3
    4 2010 3.4

    At a minimum, I want to join these tables on Site and Year to produce:

    TableA.Site TableB.Year TableB.X1 TableB.X2 TableC.X3
    1 2009 0.1 A
    1 2010 0.2 B 1.1
    2 2009 2.2
    2 2010
    3 2009 A
    3 2010
    4 2009 1.3
    4 2010 3.4

    Ideally, I want to remove rows with no data for X1, X2, or X3. and replace empty cells with a missing value code such as "NA":

    TableA.Site TableB.Year TableB.X1 TableB.X2 TableC.X3
    1 2009 0.1 A NA
    1 2010 0.2 B 1.1
    2 2009 NA NA 2.2
    3 2009 NA A NA
    4 2009 NA NA 1.3
    4 2010 NA NA 3.4

    Can anyone help? What would be the appropriate SQL code for Access?

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You first have to create a query that does a left join between table A and table B. Then create another query that does a left join between table A and table C. Then create a final query that used the 2 previous queries and join by both site and year. You will need to nest the X1, X2, and X3 within separate IIF() functions to return an "NA" when the fields are null. I've attached an example.

    BTW, the word "year" is a reserved word in Access, so it should not be used as a field or table name.

    Additionally, your tables are not structured properly, I would guess that the tables should look more like this (based on the limited info you have presented about your application)

    tblSites
    -pkSiteID primary key, autonumber
    -SiteNo

    tblDataTypes (holds 3 records: X1, X2, X3)
    -pkDataTypeID primary key, autonumber
    -txtDataType

    tblSiteData
    -pkSiteDataID primary key, autonumber
    -fkSiteID foreign key to tblSites
    -fkDataTypeID foreign key to tblDataTypes
    -myYear
    -myValue (the actual values of X1, X2, X3 etc.)

  3. #3
    Etownguy is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    2
    Thanks very much for your help. I think this will solve most, if not all, of my problems. Cheers!

  4. #4
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You're welcome; good luck with your project.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. how do i join records from two tables via a form
    By Kananelo in forum Programming
    Replies: 2
    Last Post: 02-24-2011, 02:01 AM
  2. Replies: 6
    Last Post: 02-13-2011, 06:02 PM
  3. Multiple Table Join Issue
    By tehbaker in forum Database Design
    Replies: 4
    Last Post: 10-07-2010, 01:30 PM
  4. Error on Multiple Table Join & IIF
    By cmartin in forum Queries
    Replies: 1
    Last Post: 05-21-2010, 08:58 PM
  5. Join three or more tables in many to many link
    By elicoten in forum Database Design
    Replies: 3
    Last Post: 02-04-2010, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums