Results 1 to 5 of 5
  1. #1
    MadCrow is offline Novice
    Windows 8 Access 2007
    Join Date
    Jun 2014
    Posts
    3

    Need help with outer-joins

    Hello everyone.

    I was hoping someone could help me understand outer joins better.

    I have the following tables set up :

    FormulaTable:

    Click image for larger version. 

Name:	Relationships.jpg 
Views:	5 
Size:	30.9 KB 
ID:	16865


    I am trying to query these tables so that I get all items from a formula, and if any of the ingredients have “ACID” associated with them in the IngredGroup table, I want that to show. So for this data it would look like:

    Click image for larger version. 

Name:	Result3.jpg 
Views:	5 
Size:	24.9 KB 
ID:	16868

    I put in the following query:



    Code:
    SELECT FormulaTable.FormulaName, FormulaTable.[Item#], FormulaTable.Ingredient, IngredGroup.Group
     
    FROM FormulaTable LEFT JOIN (IngredTable RIGHT JOIN (GroupTable RIGHT JOIN IngredGroup ON
                      GroupTable.Group = IngredGroup.Group) ON IngredTable.Ingred = IngredGroup.Ingred) ON
                      FormulaTable.Ingredient = IngredGroup.Ingred
     
    WHERE (((FormulaTable.FormulaName)=[X]) AND ((IngredGroup.Group)=[Y]));
    If I enter F1 and ACID for the variables, I get this:

    Click image for larger version. 

Name:	Result1.jpg 
Views:	5 
Size:	14.6 KB 
ID:	16866

    If I add:
    OR (((FormulaTable.FormulaName)=[X]) AND ((IngredGroup.Group) Is Null)) to WHERE, I get:

    Click image for larger version. 

Name:	Result2.jpg 
Views:	5 
Size:	19.6 KB 
ID:	16867

    So I’m still missing RM4 since it is tagged with “INORG” but not “ACID”. I guess I understand that part, since RM4 is not null. However I thought that with the outer joins I used, I would get everything with FormulaName = “F1”, but it seems to be behaving more like an inner join.

    Where have I gone wrong? Any quick suggestions on how to get the output I intended?

    Thank you for your time!

  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,967
    RM5 in IngredGroup shows as INORG, why is it ACID in the output? If you want to provide db for analysis, follow instructions at bottom of my post.

    Why the * character in field names? Is that just for the forum post?

    Advise no spaces or special characters/punctuation (underscore is exception) in naming convention. Better would be ItemNum or ItemNo or Item_Num.
    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
    MadCrow is offline Novice
    Windows 8 Access 2007
    Join Date
    Jun 2014
    Posts
    3
    Thank you June. Attached is db. To answer your question about RM5, the IngredGroup table is populated as (I think it didn't show right in original post):
    Click image for larger version. 

Name:	IngredGroupTable.jpg 
Views:	5 
Size:	12.8 KB 
ID:	16870

    I want it so an RM can have many groups associated.

    The * was just to designate the primary key, but I changed it to pics now. Hopefully it is clearer.
    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,967
    The RM and many groups doesn't make sense to me and will probably cause lots of issues. But consider:

    Query1
    SELECT FormulaTable.FormulaName, FormulaTable.[Item#], FormulaTable.Ingredient, IngredGroup.Group
    FROM IngredTable RIGHT JOIN (FormulaTable LEFT JOIN IngredGroup ON FormulaTable.Ingredient = IngredGroup.Ingred) ON IngredTable.Ingred = IngredGroup.Ingred
    WHERE (((IngredGroup.Group)="ACID"));

    Query2
    SELECT FormulaTable.FormulaName, FormulaTable.[Item#], FormulaTable.Ingredient, FormulaTable.Amount, Query1.Group
    FROM Query1 RIGHT JOIN FormulaTable ON (Query1.[Item#] = FormulaTable.[Item#]) AND (Query1.FormulaName = FormulaTable.FormulaName);
    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.

  5. #5
    MadCrow is offline Novice
    Windows 8 Access 2007
    Join Date
    Jun 2014
    Posts
    3
    That seems to work perfectly! Thank you very much June!

    As for the way I have my groups, I will have an unlimited number of groups I may want to assign to any particular Ingredient. I though this was the cleanest way to do it, but I will re-think that now.

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

Similar Threads

  1. Merging two queries of data using outer joins
    By scorpio in forum Queries
    Replies: 1
    Last Post: 02-02-2014, 12:58 PM
  2. Replies: 15
    Last Post: 10-22-2012, 06:06 PM
  3. ambiguous outer joins
    By libraccess in forum Queries
    Replies: 5
    Last Post: 03-31-2012, 05:41 PM
  4. LEFT/RIGHT/OUTER JOINS in a set of subqueries
    By luckycharms in forum Queries
    Replies: 1
    Last Post: 08-01-2011, 05:06 PM
  5. Multiple outer joins - error message
    By Lipi in forum Queries
    Replies: 1
    Last Post: 09-16-2010, 02:44 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