• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 775
  • Last Modified:

Ambiguous Outer Joins

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
doctortony
Asked:
doctortony
  • 5
  • 3
1 Solution
 
sebastienmCommented:
doctortony,
Could you post the SQL (In the query Designer window,
 right-click the window and choose SQL).
Thanks,
Sébastien
0
 
sebastienmCommented:
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
 
doctortonyAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sebastienmCommented:
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
 
doctortonyAuthor Commented:
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
 
sebastienmCommented:
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
 
doctortonyAuthor Commented:
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
 
sebastienmCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now