Solved

Ambiguous Outer Joins

Posted on 2002-03-18
8
733 Views
Last Modified: 2008-03-17
I do a report periodically that uses 3 semesters of course failure data.  The data for each semester is brought into Access as a separate table: e.g., Jan01, Jun01, Jan02.  The report will use several fields from the current table—in this case, Jan02—and one field from each of the earlier tables, Jan01 and Jun01.  I want to use a make table query to bring the selected fields from each table into a single table.  

In each table, the primary key is a composite of the school’s 4-digit unit number and 2-digit grade (called UntGrd); so, e.g., 101009 signifies 9th grade in unit 1010.  

I have no problem designating UntGrd as the primary key in each table, which implies that the values for UntGrd are unique in each table.  I have no problem relating the tables and enforcing referential integrity.  The most recent table (Jan02) is the primary table.  Therefore, referential integrity implies that all records in Jan01 and Jun01 are in Jan02.  There are no “orphans” in Jan01 and Jun01 although “childless parents” are permitted in Jan02.  The joins from Jan02 to Jan01 and Jun01 are left outer joins (all records in Jan02 and only the matching records in Jan01 and Jun01).

My problem is that when I run the make table query, I get the message “The SQL statement could not be executed because it contains ambiguous outer joins.  To force one of the joins to be performed first, create a separate query that performs the first join and then include that query in you SQL statement.”  I would expect the joins to be performed in the order that tables are called into the query; so, I obviously don’t understand the problem.  

Doing a work-around is easy enough.  I can run a make table query to join any two tables, say Jan01 and Jun01, to produce Temp01.  Then, I can run another make table query to join Temp01 and Jan02.  Fine, but I still would like to understand that error message.  100 points to anyone who can explain it.
0
Comment
Question by:doctortony
  • 5
  • 3
8 Comments
 
LVL 16

Expert Comment

by:sebastienm
ID: 6878303
doctortony,
Could you post the SQL (In the query Designer window,
 right-click the window and choose SQL).
Thanks,
Sébastien
0
 
LVL 16

Expert Comment

by:sebastienm
ID: 6878827
Doctortony,

Assuming you want to retrieve the primary key, and the
field F2 that is in all your tables, into a new table
called NewTableName, you should have the following SQL
statement:
------------------------------------------------------
SELECT
    Jan02.UntGrd,
    Jan02.F2,
    Jun01.F2 AS F22,
    Jan01.F2 AS F23
INTO
   NewTableName
FROM
   (Jan02 LEFT JOIN Jun01 ON Jan02.UntGrd = Jun01.UntGrd)
   LEFT JOIN Jan01 ON Jan02.UntGrd =Jan01.UntGrd
;
-------------------------------------------------------
F22 new name in new table for field Jun02.F2
F23 new name in new table for field Jan01.F2
You should see something similar in your query SQL.
Depending on your field/table names, you'll have brackets
or apostrophes surrounding the field/table names.
Note that you can edit the SQL statement (if you think
Access did not translate correctly) then, when going
back to Design window, Access will update the query from
the SQL you just edited (before editing, copy/paste the
query with another name,... in case ...)

Hope this help,
Sébastien
0
 

Author Comment

by:doctortony
ID: 6880618
Sebastienm,
If I right click in the query designer window and choose SQL View, it does not show me the SQL.  Instead, it tries to execute the query, and it returns the error message about ambiguous joins.
0
 
LVL 16

Expert Comment

by:sebastienm
ID: 6881238
Can you tell me the fields you want from these tables.
I'll create the SQl string then send it back to you, then:
-Create a new blank query in Access
-In SQL View, paste the SQL string
-Go back to query designer
--> this should translate the SQL into the usual diagramm

I'll also try to save the Query and send it to you, so
that you can import it. If the table/field names are the
same, it should work.

Sébastien
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:doctortony
ID: 6882763
Sebastienm,
If it would help, I could email you an abbreviated copy of the Access database (all fields but only a few records per table).  You can email me directly at apitruzzello@csc.cps.k12.il.us.  What I'm really interested in is not just solving the problem but understanding the logic.  Given that Access accepts my attempt to enforce referential integrity, I don't see why the query sees the joins as "ambiguous."  I think it has something to do with the fact that the relationships among tables are 1-to-1.  But that's as far as I get.
0
 
LVL 16

Accepted Solution

by:
sebastienm earned 100 total points
ID: 6884338
Doctortony,
I got your email & dB.

I noticed that, in the query, there are 3
links, not 2 as you said in your first post.
    Table3 (Jan02)  --->  Table1 (Jan01)
    Table3 (Jan02)  --->  Table2 (Jun01)
and Table1 (Jan01)  --->  Table2 (Jun01)
It's hard to see in the Diagram because the tables are
aligned so the links are supperposed.
--> THIS is the problem

So:
- In the diagram, Move your tables around to be able to see
  all 3 links
- Delete the link from Table1(Jan01) and Table2(Jun01)
that's it.

The engine doesn't like when the links form a loop
(closed-path) because it doesn't know which path to take
to return the data (Table2 data could be accessed trough
table 1 or table2). Some databases have optimizers for
these situations. But it should be avoided.
Sometimes you really need the table twice, eg:

Shipment Table (S)
--------------
-number
-weight
-origin zip
-dest customer

Customer Table (C)
-------------
-name
-zip

Location Table (L)
---------------
-zip
-city
-state

you want shipment number, origin city, destination city
S.o_zip --- L.zip
S.dest_cust --- C.name
C.zip ---- L.zip
3 links but it makes a closed-path (loop)

So, what you should do, is to define the L table twice
(in Access, it is like putting the table L twice in the
diagram, the name of the second one will change to L_2)
Then:
S.o_zip --- L.zip
S.dest_cust --- C.name
C.zip ---- L_2.zip

Hope this will fix the problem,
Sébastien
0
 

Author Comment

by:doctortony
ID: 6889423
Sebastienm,
Thanks a million!  What puzzles me is that, if you check the relationships window in the DB I sent you, you'll notice that there are only two relationships: Table3-->Table1 and Table3-->Table2.  When I reconstructed the query (just now), I noticed that when I selected the three tables, indeed, Table1 was linked to Table2!  I always thought that once the relationships were defined (in the relationships window), they were set.  Any query acted on the relationships as they were set.  I didn't know that the act of constructing a query could somehow insert a new relationship independent of what was specified in the relationships module.  And, you're right, I did not see the link between Tables 1 and 2.  Because I thought the relationships were set, I could have sat there till Hell freezes over, and I would not have seen the problem.  Thanks, again.
0
 
LVL 16

Expert Comment

by:sebastienm
ID: 6889488
Thank you Tony.

Access tend to link fields with identical names eventhough
 the relationaships are set.

In menu Tools>Option, tab Tables/Queries, there is an
option called AutoJoin. Unchecking it may cancel this
behavior (not sure)

From MS help:
"Access automatically creates joins if you add two tables
to a query and the tables each have a field with the same
or compatible data type and if one of the join fields is a
primary key"

Regards,
Sébastien
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now