Results 1 to 10 of 10
  1. #1
    ltcarlisle is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    23

    Conditional joins

    Hi all,

    I have two tables. Both have first names and last names in each record and each record in each table has a unique ID. Table A has a city and business name for each record, but in Table B only some entries have cities and business names, some only have cities, some only have business names and some don't have either. I'd like to set up a conditional join between tables A and B, one that follows this logic:

    If table B's record (denoted by first/last name combo) has BOTH a city and business name, join to table A's record on first name/last name/city/business name.



    If table B's record has ONLY a city, join to table A's record on first name/last name/city.

    If table B's record has ONLY a business name, join to table A's record on first name/last name/business name.

    --------------------------------------------------------------------------------------------------------------------

    Is this possible?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    The unique ID is not a linking value?

    Joins cannot be different for each record. The join is established between fields of tables/queries. Either field is included or it isn't. Can't be dynamic dependent on record.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    hapm is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    197
    Join by first and last name and filter the additional unwanted results with where clause
    or
    Use multiple queries and union the results

  4. #4
    ltcarlisle is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    23
    I could do this, but when I join only by first and last name there are incorrect matches between people with the same first and last name.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    And therein lies the conundrum caused by bad data.

    Looks like will need to UNION 3 SELECT queries with appropriate INNER JOIN.

    Can be done in one SQL statement built into the SQL View of query builder.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    hapm is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    197
    As I said you can filter these matches using the where clause.

  7. #7
    ltcarlisle is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    23
    I'm having trouble understanding how either of these strategies could work. Could you provide some example code?

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Build 3 queries with the joins you describe in first post. Use INNER JOIN and resulting dataset will show only records that have exact matches on each of the joined fields.

    Then if you want to view the 3 queries as one combined dataset, build a UNION query. There is no wizard or designer for UNION, must type into SQL View of query builder.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    hapm is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    197
    SELECT A.*, B.* FROM A INNER JOIN B ON A.FirstName = B.FirstName AND A.LastName = B.LastName WHERE (A.City IS NULL OR A.City = B.City) AND (A.BusinessName IS NULL OR A.BusinessName = B.BusinessName)

  10. #10
    ltcarlisle is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jun 2014
    Posts
    23
    Thank you both for the help

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

Similar Threads

  1. Replies: 6
    Last Post: 05-26-2013, 09:10 PM
  2. Replies: 1
    Last Post: 11-14-2011, 07:03 PM
  3. Joins
    By jlgray0127 in forum Forms
    Replies: 2
    Last Post: 11-11-2011, 05:04 PM
  4. Need Help with Joins
    By usa_dreamer2002 in forum Queries
    Replies: 3
    Last Post: 01-31-2011, 10:58 AM
  5. Complex Joins??
    By objNoob in forum Queries
    Replies: 0
    Last Post: 03-16-2010, 02:42 PM

Tags for this Thread

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