Go Premium for a chance to win a PS4. Enter to Win


SQL Question

Posted on 2002-05-07
Medium Priority
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:

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

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

The results should look like this:

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?

Question by:beercat2
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 200 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));

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.

LVL 143

Expert Comment

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

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));

LVL 43

Expert Comment

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


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
LVL 46

Expert Comment

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


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

Author Comment

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!
LVL 46

Expert Comment

ID: 6996542

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.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

926 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