beercat2
asked on
SQL Question
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
to explain:
NULL = NULL
-> returns false !!!!
This is true for other database systems also.
CHeers
NULL = NULL
-> returns false !!!!
This is true for other database systems also.
CHeers
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));
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));
Select Query1.Area,Query1.Machine ,QUery1.Un itOp,Query 1.Mode,Que ry1.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.Un itOp,Query 1.Mode,Que ry1.Occur, Query1.DT, UT = (Select Query2.UT From Query2 Where Query2.Machine = Query1.Machine And Query2.Area = Query1.Area) From Query1
Or
Select Query1.Area,Query1.Machine
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
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
ASKER
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!
Thanks for all of your expertise!
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.
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.
Anthony