Solved

SQL Question

Posted on 2002-05-07
8
150 Views
Last Modified: 2010-05-02
This is killing me because I'm certain I should know why this isn't working, but I can't seem to figure it out.  I have two queries in an Access database that I'm trying to join into 1 query that I can use as the data source for a report.  Here are the 2 queries:

Query1
Area  Machine   UnitOp  Mode  Occur  DT
----  -------   ------  ----  -----  -----
NULL  MachA     NULL    Leak  8      6.7
NULL  MachA     NULL    Can   4      0.28

Query2
Area  Machine   UT
----  -------   -------
NULL  MachA     331.497

The results should look like this:

Query3
Area  Machine   UnitOp  Mode  Occur  DT      UT
----  -------   ------  ----  -----  -----   -------
NULL  MachA     NULL    Leak  8      6.7     331.497
NULL  MachA     NULL    Can   4      0.28    331.497

However, when I run my query, either nothing from Query1 or Query2 shows, depending on which JOIN I use.  For example, originally my SQL looked like this:

SELECT Query1.*, Query2.UT
FROM Query1 LEFT JOIN Query2 ON (Query1.Machine = Query2.Machine) AND (Query1.Area = Query2.Area);

When I ran this, the data from Query1 showed up just fine, but the data from Query2 did not.  So, just to test, I switched to a RIGHT JOIN and the data from Query2 showed up just fine, but the data from Query1 didn't show up.  Does anyone see anything wrong with my queries or have a suggestion of how to better accomplish this?

Thanks!
Brian
0
Comment
Question by:beercat2
8 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 6994104
The problem is the NULL value:

SELECT Query1.*, Query2.UT
FROM Query1 JOIN Query2 ON (Query1.Machine = Query2.Machine) AND (IIF(Query1.Area IS NULL, 999,Query1.Area) = IIF(Query2.Area IS NULL, 999, Query2.Area));

CHeers
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6994111
I could be wrong, but I don't believe you can join fields that are Null.  In other words if the value is Null than it will not be included.

Anthony
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6994117
to explain:
NULL = NULL
-> returns false !!!!
This is true for other database systems also.
CHeers
0
Technology Partners: 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!

 
LVL 18

Expert Comment

by:bobbit31
ID: 6994122
i'm guessing the problem is here:
Query1.Area = Query2.Area

because you are doing where null = null which should probably be null is null so maybe modify query to be:

SELECT Query1.*, Query2.UT
FROM Query1 LEFT JOIN Query2 ON (Query1.Machine = Query2.Machine) AND ((Query1.Area is null AND Query2.Area is null) OR (Query1.Area = Query2.Area));



0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6994124
Select Query1.Area,Query1.Machine,QUery1.UnitOp,Query1.Mode,Query1.Occur,Query1.DT,Query2.UT From Query1 Inner Join Query2 On Query1.Machine = Query2.Machine And Query1.Area = Query2.Area

Or

Select Query1.Area,Query1.Machine,QUery1.UnitOp,Query1.Mode,Query1.Occur,Query1.DT,UT = (Select Query2.UT From Query2 Where Query2.Machine = Query1.Machine And Query2.Area = Query1.Area) From Query1
0
 
LVL 45

Expert Comment

by:aikimark
ID: 6995509
SELECT Query1.*, Query2.UT
FROM Query1 LEFT JOIN Query2 ON (Query1.Machine = Query2.Machine)
Where Query1.Area Is Null and Query2.Area Is Null

UNION

SELECT Query1.*, Query2.UT
FROM Query1 LEFT JOIN Query2 ON (Query1.Machine = Query2.Machine) AND (Query1.Area = Query2.Area)
Where Query1.Area Is Not Null and Query2.Area Is Not Null
0
 
LVL 1

Author Comment

by:beercat2
ID: 6996485
Wow!  Thanks for all the quick replies.  Since I'm certain all of these would have worked, I'm just going to award the points to the first answer.

Thanks for all of your expertise!
0
 
LVL 45

Expert Comment

by:aikimark
ID: 6996542
beercat2,

Please verify answers before accepting them as valid.  Every suggested answer may NOT solve your problem.  Not all solutions are of equal value in all environments.  We experts only see the "tip of the iceberg" part of your problem and the easiest-to-produce answer might not be your best (production) solution.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 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