Results 1 to 4 of 4
  1. #1
    oemar00 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    14

    Left OUTER Join to more than one table?

    Please see the attched, but here is the explanation:
    Hello,I have three tables tblA, tblAB, and tblB.
    tblAB has three fields AB_ID (PK), A_Attr, and B_Attr.
    tblA has two fields A_ID (PK) which contain the same value as A_Attr, and field A_Date.
    tblB has two fields B_ID (PK) which contain the same value as B_Attr, and B_Date.
    tblA has one to many relation to tblAB through A_ID to A_Attr, tblB has one to many relation to tblAB through B_ID to B_Attr.
    If A_Attr in tblAB is not NULL the value will be one of A_ID, the same if B_ Attr in tblAB is not NULL the value will be one of B_ID.
    A_Attr and B_Attr in tblAB could be nulls, A_ID or B_ID might contain value not yet in tblAB.


    I want to list all AB_ID and the corresponding date from tblA and tblB if they exist.
    With one table I can do OUTER LEFT JOIN to either table (see the query in the attached) which will list all AB_ID and either A_Date or B_Date but how do I list all AB_ID, plus A_Date if existed and also B_Date if it existed.
    If for that AB_ID neither A_Attr nor B_Attr exist I still want to see the AB_ID with NULL in the A_Date or B_date whatever the case might be.
    I tried with LEFT JOIN followed by another LEFT JOIN but Access give a message saying my LEFT JOIN is not valid.
    I thought the solution should be simple but at the moment I could not get my mind to it.
    Any solution is appreciated, thank you.
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Maybe:
    SELECT tblAB.AB_ID, tblAB.B_Attr, tblB.B_Date, tblA.A_Date
    FROM tblA RIGHT JOIN (tblB RIGHT JOIN tblAB ON tblB.B_ID = tblAB.B_Attr) ON tblA.A_ID = tblAB.A_Attr;

    Right now the data represents one-to-one relationship of tblAB to each of tblA and tblB. Is this the actual situation? If not, I expect cannot join tblA and tblB to tblAB and achieve satisfactory results.

    Why aren't tblA and tblB just one table?
    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
    oemar00 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    14

    The solution works!

    The relationship diagram and the explanation clearly says "tblA has one to many relation to tblAB through A_ID to A_Attr, tblB has one to many relation to tblAB through B_ID to B_Attr."
    There is no relationship between tblA and tblB the Date field in those table is related only to their Primary Key, so tblA and tblB can not be on the same table.

    The sample I sent was not good enough however the solution works correctly!!!

    I have added a better sample of what I need but the solution is working in both cases.

    With your stetement here "If not, I expect cannot join tblA and tblB to tblAB and achieve satisfactory results." Could you tell me the situation where the solution will not work?
    Attached Files Attached Files

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I missed that statement in your narrative and table relationships (which I did not open to look at) aren't always set properly for the nature of data, so I asked.

    Works because tblA and tblB records are unique. Not a parent/child relationship with tblAB as I initially thought. Now I am looking at the ERD and see that tblAB is a junction table. tblA and tblB have a many-to-many relationship. tblAB associates records of tblA and tblB.
    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.

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

Similar Threads

  1. Left-Outer Join on Non-Unique ID
    By defaultuser909 in forum Queries
    Replies: 2
    Last Post: 09-06-2012, 10:16 AM
  2. Replies: 4
    Last Post: 09-03-2012, 04:53 PM
  3. LEFT/RIGHT/OUTER JOINS in a set of subqueries
    By luckycharms in forum Queries
    Replies: 1
    Last Post: 08-01-2011, 05:06 PM
  4. Replies: 3
    Last Post: 02-02-2011, 01:00 PM
  5. Outer left join leaves out null values
    By mschles4695 in forum Queries
    Replies: 1
    Last Post: 12-22-2010, 11:43 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