Solved

SQL Question

Posted on 2002-05-07
8
149 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

790 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