Solved

SQL Question

Posted on 2002-05-07
8
146 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 142

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 142

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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

746 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