Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Question

Posted on 2002-05-07
8
Medium Priority
?
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 143

Accepted Solution

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

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

 
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 46

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 46

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

Independent Software Vendors: 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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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 …
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…
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

715 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