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

    Query need more attributes from other tables

    Hello,


    Refer to the attached file, I have five tables tblA, tblB, tblC, tblX, and tblY.
    tblA related to tblB many-to-many through tblC, I found records in tblA with no related record in tblB by executing qryA_Missing_C in the attached file:
    Code:
    SELECT tblA.ID_A
              , tblA.AttrX_A
              , tblA.AttrY_A
              , tblA.Attr01_A
              , tblA.Attr02_A
              , tblA.Attr03_A
    FROM tblA
    WHERE ID_A 
                 NOT IN 
                   (SELECT ID_CA FROM tblC
                                 WHERE ID_CA = ID_A)
    ORDER BY 1;
    I want to add attributes from tblX and tblY to the result by executing qryA_Missing_C02 resulting in no records found. There are records in tblX and tblY that matches the result query qryA_Missing_C but even if the record does not exist in either tblX or tblY it should still return NULL in the attributes. The syntax might got me on this one. Changing INNER JOIN into LEFT JOIN does not work, Access consider that as invalid statement. There is similarity with the one I post before however the difference is significant enough that I cannot use the same formula as before.
    Code:
    SELECT tblA.ID_A
              , tblA.AttrX_A
              , tblA.AttrY_A
              , tblA.Attr01_A
              , tblX.X_Date
              , tblY.Y_Date
              , tblC.ID_CB
              , tblC.ID_CA
    FROM tblB 
    INNER JOIN ((tblY INNER JOIN (tblX 
                                  INNER JOIN tblA ON tblX.X_ID = tblA.AttrX_A) ON tblY.Y_ID = tblA.AttrY_A) 
                                  INNER JOIN tblC ON tblA.ID_A = tblC.ID_CA) ON tblB.ID_B = tblC.ID_CB
    WHERE (Not (tblC.ID_CA)=[tblA].[ID_A]);
    Thank you again.
    Attached Files Attached Files

  2. #2
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Okay, I have no idea what you think that second SQL statement has to do with the first one. The first one requires that there be no matching record in tblC, but the second requires that tblB be INNER JOINED to the tblC record that doesn't exist in the first one. You can't get a reasonable result that isn't empty.

    You can't get matching attributes off tblB based on a match with tblC when you want tblA records that are not in tblC.

    Try something like this -
    Code:
    SELECT 
       tblA.ID_A,
       tblA.AttrX_A,
       tblX.X_Date,
       tblA.AttrY_A,
       tblY.Y_Date,
       tblA.Attr01_A,
       tblA.Attr02_A,
       tblA.Attr03_A
    FROM 
       (((tblA 
          LEFT JOIN tblC ON tblC.ID_CA = tblA.ID_A)
         LEFT JOIN tblX ON tblX.X_ID = tblA.AttrX_A)
        LEFT JOIN tblY ON tblY.Y_ID = tblA.AttrY_A) 
    WHERE tblC.ID_CA Is Null
    ORDER BY tblA.ID_A;

  3. #3
    oemar00 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    14
    Dal Jeanis,
    You are correct, this solution work. After you show the solution is seems easy but not at the time when I was looking for a solution.

    Thank you.

  4. #4
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    oemar - yep, I know how that feels. Lots of times I've tried method after method until I've forgotten what exactly my goal was.

    In fact, every time I have to do an INNER JOIN and a LEFT JOIN in the same SQL, I have to google up which ways Access will allow it.

    From http://msdn.microsoft.com/en-us/libr...ffice.12).aspx
    A LEFT JOIN or a RIGHT JOIN can be nested inside an INNER JOIN, but an INNER JOIN cannot be nested inside a LEFT JOIN or a RIGHT JOIN. See the discussion of nesting in the INNER JOIN topic to see how to nest joins within other joins.
    If I can arrange it so that everything is LEFT JOINS, as I did here, then I know it should work.

    Another useful strategy is found here - http://stackoverflow.com/questions/5...n-in-ms-access
    If you need LEFT for the first join and INNER for the second, you can move the tables from the second join into a subquery.

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

Similar Threads

  1. Replies: 5
    Last Post: 09-25-2013, 02:20 PM
  2. Derived attributes
    By mlbwhf in forum Access
    Replies: 8
    Last Post: 11-06-2012, 09:07 PM
  3. Replies: 9
    Last Post: 07-06-2012, 10:43 AM
  4. attributes out of order
    By Dornenhexe in forum Queries
    Replies: 9
    Last Post: 07-12-2010, 02:36 PM
  5. Hidden Attributes
    By NMJones in forum Access
    Replies: 1
    Last Post: 02-09-2010, 10:57 AM

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