Results 1 to 4 of 4
  1. #1
    krausr79 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    16

    Left join data into 0, 1, or 2 columns depending on flag

    Suppose I have data in my main table:


    A
    B
    C
    D

    And data in a second table that should attach to the main data in one of two ways (X or Y):
    A 1 X
    A 2 Y
    B 3 X
    C 4 Y
    The secondary table records will be unique per main data and X/Y.

    Then I'd like to make a query that shows all main table data with attached data:
    A 1 2
    B 3
    C 4
    D

    I feel like this should be easy but it's giving me trouble. I'm currently left joining in two successive queries, but all the conditions I've tried have left things out. I can't find the correct google language for this particular problem.

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    try something like

    Code:
    SELECT m.fld1, x.fld2, y.fld2
    FROM (main as m left join secondary as x ON m.fld1=x.fld1) left join secondary as y on m.fld1=y.fld1

  3. #3
    krausr79 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    16
    That one gives me two lines for things with both kinds of data. I'm having success with two extra queries on the secondary data: One to get X data and one to get Y data. Then I can left join both of them to the main data with no restrictions.

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    OK then the code would be

    Code:
    SELECT m.fld1, x.fld2, y.fld2
    FROM (main as m left join secondary as x ON m.fld1=x.fld1) left join secondary as y on m.fld1=y.fld1
    WHERE x.fld3="X" and y.fld3="Y"

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

Similar Threads

  1. Left Join
    By MrJukas in forum Access
    Replies: 13
    Last Post: 10-21-2015, 10:27 AM
  2. Inner Join works but not Left Join
    By kwooten in forum Queries
    Replies: 1
    Last Post: 12-11-2012, 05:09 PM
  3. Replies: 7
    Last Post: 09-06-2012, 06:04 AM
  4. Left Join not
    By j_a_monk in forum Queries
    Replies: 5
    Last Post: 08-07-2011, 09:47 AM
  5. Replies: 3
    Last Post: 02-02-2011, 01:00 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