Solved

Combine Left and Right Join

Posted on 2006-11-17
6
311 Views
Last Modified: 2008-02-26
Hello Experts,

I need some help with a query.   My db design currently contains 3 levels of entities (following principle of grandparent, parent, child).

#1. "GrandParent" and "Parent" are linked via Junction Table
#2. "Parent" and "Grandchild" are linked via Junction Table

In most cases, I have "complete records" where all 3 levels are linked top to bottom.   However, in some instances, I may only have records of "scenario" #1... while in other cases I only have those of #2.

To find the missing Grandparent, I use a Left join query... starting from the parent level.
To find the missing Grandchild, I use a Right join query... also starting the the parent level.

Not sure if it's possible, but I'd like to combine both Right and Left join query in a single query (vs. using two queries like right now).

I tried the UNION but that doesn't seem to work.   Hopefully I provided enough info in order to get some general ideas as to how I can accomplish it.

Thanks,
EEH


0
Comment
Question by:ExpExchHelp
  • 3
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17965008
yes, both left and right join are combinable. however, I find the usage of right join difficult to handle.

anyhow   A LEFT JOIN B  is the same as B RIGHT JOIN A
0
 
LVL 12

Expert Comment

by:koutny
ID: 17965236
To summarize the syntax, it would be something like

select field_names FROM (ParentTable LEFT JOIN GrandChildTable ON ParentTable.PK_Field = GrandChildTable.FK_Field) LEFT JOIN GrandParentTable ON ParentTable.FK_Field = GrandParentTable.PK_Field

I have just noticed that you use junction tables (assuming n to n relationships between tables?). So that would make it just slightly more complicated:

select field_names FROM (ParentTable LEFT JOIN (JunctionTableParentGrandChild INNER JOIN GrandChildTable ON JunctionTableGrandChild.GrandChildPK_Field = GrandChildTable.PK_Field) ON ParentTable.PK_Field = JunctionTableParentGrandChild.ParentPK_Field)
   LEFT JOIN  (JunctionTableGrandParent INNER JOIN GrandParentTable ON JunctionTableGrandParent.GrandParentPK_Field =  GrandParentTable.PK_Field) ON ParentTable.PK_Field = JunctionTableGrandParent.ParentPK_Field

0
 

Author Comment

by:ExpExchHelp
ID: 17967326
Okay, I kinda expected that it might be challenging to come to a solution via just explaining it.  So, I've uploaded a
sample db at (I forgot how to get to the EE upload site).  If the one below doesn't work, please tell me how to get to EE uploads (thanks!).

http://www.bestsharing.com/files/ms001174543/Listbox%20v02.zip.html

If you feel comfortable downloading it, here are the steps to make sense of the problem:


1. Background Info:
===================

Again, I have 3 entity levels (grandparent, parent, child).  The db's equivalent are:

a. PAR = grandparent
b. WL  = parent
c. Issue = child


2. Opening up the Sample db:
============================

The frmLogin is my default form when opening the db.  I'm sure you know, but just in case... to get "behind the scene" please press/hold the SHIFT key.


3. Viewing the Db:
==================

a. now, the frmLogin has 2 listboxes:  "Data Entry" and "Create Linkages"

b. first, if you were to look at each of the three data entry forms you'd find the following records counts:

-- "Create PAR": contains 4 records (PAR 1, PAR 2, PAR 3, PAR 4)
-- "Create WL":  contains 6 recors (WL 1, WL 2, ..., WL 6)
-- "Create Issue": contains 9 records (Issue 1, Issue 2, ..., Issue 9)


c. then, the 2nd listbox create two forms to "make the connection" between >> Parent and Grandparent (WL to PAR) << as well as >> "Grandchild to Parent (Issue to WL)" <<

- More specifically, if you select on form "Link WL to PAR", you'll see the 4 PARs in the dropdown.
- also, you find all "available" (unassigned) WL in the left listbox.  All WLs that are linked to a PAR is shown in the
right listbox.
- the same concept is applied for the 2nd form "Link Issue to WL".  Except, in this case the WLs are in the drop-down
menu and the Issues are shown in the 2 listboxes.

Make sense so far???   I hope...


4. Now, viewing the report "View all Data"... THIS IS WHERE THE ACTUAL PROBLEM LIES...
======================================================================================

- at first look, the report appears fine.  It shows the 3-tier entities in top-down (left-right) view

- however, having a closer look (in conjunction with the 2 forms where I created the linkages), some records are not
shown.  This is where the combination of joining the LEFT/RIGHT join will come handy.


- okay, what's the problem:

-- first, I suggest to print out the report "View All Data"
-- now, open form "Link WL to PAR"
-- by default, "PAR 1" is shown.  It has one child record... "WL 2"... comparing it w/ my report, it looks fine
-- now, I select "PAR 2" from the drop-down menu... it has 2 children... "WL 1" and "WL 3"... again, comparing it to
the report, it appears fine.
-- finally, if I'd select "PAR 3" and/or "PAR 4"... I don't see any children.   Okay, here's the first problem, my report doesn't show me "PAR 3" and "PAR 4".  While these two grandparent-level records don't have any children (WLs), I still want to show them in the top portion of the report.   So, that's where I'd apply the Right-Join in my query.


-- now, I'd do a similar review of the data shown in form "Link Issue to PAR".  I won't go into details, but you get the idea... I'm simply comparing (with my report) which WLs have which children (Issues).   I'm using a Right-Join to show the "---" (Issue level) for the "WL 3".



Okay, I may already have provided too much details for you to make sense of... hopefully you didn't mind and didn't cause any great confusion.



5. Here's what it bulls down to:
================================

The report "View All Data" should show me all existing records... whether linked or not linked.   Currently, it doesn't... for instance, "PAR 2" and "PAR 4" are missing.  

How can I fix it?


Thanks,
EEH
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 12

Accepted Solution

by:
koutny earned 500 total points
ID: 17967710
Now, I have only had a quick look at your file (I am based in the UK and it's getting towards 7pm here on a Friday evening so it's about time I leave the office).
As far as I can tell, the query which is the record source for your report (qryQuickSnapshot) is doing what you would except it to do.

That is, show all the WL records and only those records from PAR or Issue tables for which there is a link from a WL record.
So naturally you can't except to see records "PAR 3" or "PAR 4" because they are not linked to any WL record. I didn't check whether there are any missing Issues records but they would be if there are any Issues without a link to a WL record.

The easy situation for you would be if a WL record always had an associated PAR record, and similarly if all Issues records had a WL record.
Clearly this is not the case. So my first thoughts are that you create 3 queries.
Your existing query would be the first one.
The second query would list all PAR records without any linked WL records.
The last query would list all Issues records without any linked WL records.

Then I would create a union query which would just merge all these three queries together. This query then would be used as the record source for your report.

To give you an idea (while perhaps wasting my breath) how to construct the second query:

SELECT [PAR] as PARIfNul, "---" as Watchlist,"---" as IssueIfNull, "---"   AS RecommendationIfNull
FROM tblPAR LEFT JOIN tblPARWLLink ON tblPAR.PARIDpk = tblPARWLLink.PARIDpk WHERE  tblPARWLLink.PARIDpk  is null
ORDER BY  [PAR]

This query shows all PAR records which don't have a link to a WL record and is useable in a union query together with the qryQuickSnapshot query.

That's off hand suggestion. Perhaps some other guy might beat me to it.

Have a nice weekend.

 
0
 

Author Comment

by:ExpExchHelp
ID: 17969438
koutny,

that works absolutely PERFECT!!!

Thanks so much for that idea.   I truly appreciate it.

EEH
0
 
LVL 12

Expert Comment

by:koutny
ID: 17971027
Well, I am glad that I could have been of some help.
Thanks for the points!
0

Featured Post

IT, Stop Being Called Into Every Meeting

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!

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

708 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

12 Experts available now in Live!

Get 1:1 Help Now