D
Don
In an earlier post I was having problems with a LEFT JOIN not including left
table records when there were no associated right table records. The
problem being in the WHERE clause:
WHERE P.PositionLocation IN ( strPositionLocation )
AND R.PersonStatus In (strPersonStatus)
John Spenser suggested modifying the where clause to:
WHERE P.PositionLocation IN ( strPositionLocation )
AND ((R.PersonStatus IS Null) OR (R.PersonStatus In (strPersonStatus))
which did the trick. Or so I thought. While it picks up the cases where a
left record has no associated right table records, I noticed the query
result lacks results where a left record has an associated right table
record, but that right table record does not satisfy the IN clause.
Here is a simple example of what I am trying to achieve:
tblLeft
ID Shape Dimensions
1 Line 2
2 Circle 2
3 Sphere 3
4 Cube 3
5 Square 2
6 Cone 3
7 Triangle 2
8 Tube 3
9 Dot 1
tblRight
Product LeftID Color
A 1 Red
B 1 Green
C 2 Blue
D 3 Green
E 5 Red
Query Result for
.....
FROM tblLeft AS L LEFT JOIN tblRight AS R ON L.ID = R.LeftID
WHERE L.Dimensions IN (1, 2) AND ((R.Color IS Null) OR (R.Color IN ("Green",
"Blue")))
Yields:
ID Shape Dimensions Product Color
1 Line 2 B
Green
2 Circle 2 C
Blue
3 Sphere 3
4 Cube 3
5 Square 2
6 Cone 3
7 Triangle 2
8 Tube 3
9 Dot 1
The "problem children" being like Product "E" (right table) that meet all
the criteria except color. In the actual result for the query - based on my
observations - ID 9 would not be included in the query.
This is where my SQL skills really break down (being a novice, that does not
take much!). My guess is that I really want a subquery to get only those
"right" records that match the "right" criteria I want. These results would
then be the right side of the LEFT JOIN clause.
Any thoughts (other than I'm crazy )?!
Thanks!
Don
table records when there were no associated right table records. The
problem being in the WHERE clause:
WHERE P.PositionLocation IN ( strPositionLocation )
AND R.PersonStatus In (strPersonStatus)
John Spenser suggested modifying the where clause to:
WHERE P.PositionLocation IN ( strPositionLocation )
AND ((R.PersonStatus IS Null) OR (R.PersonStatus In (strPersonStatus))
which did the trick. Or so I thought. While it picks up the cases where a
left record has no associated right table records, I noticed the query
result lacks results where a left record has an associated right table
record, but that right table record does not satisfy the IN clause.
Here is a simple example of what I am trying to achieve:
tblLeft
ID Shape Dimensions
1 Line 2
2 Circle 2
3 Sphere 3
4 Cube 3
5 Square 2
6 Cone 3
7 Triangle 2
8 Tube 3
9 Dot 1
tblRight
Product LeftID Color
A 1 Red
B 1 Green
C 2 Blue
D 3 Green
E 5 Red
Query Result for
.....
FROM tblLeft AS L LEFT JOIN tblRight AS R ON L.ID = R.LeftID
WHERE L.Dimensions IN (1, 2) AND ((R.Color IS Null) OR (R.Color IN ("Green",
"Blue")))
Yields:
ID Shape Dimensions Product Color
1 Line 2 B
Green
2 Circle 2 C
Blue
3 Sphere 3
4 Cube 3
5 Square 2
6 Cone 3
7 Triangle 2
8 Tube 3
9 Dot 1
The "problem children" being like Product "E" (right table) that meet all
the criteria except color. In the actual result for the query - based on my
observations - ID 9 would not be included in the query.
This is where my SQL skills really break down (being a novice, that does not
take much!). My guess is that I really want a subquery to get only those
"right" records that match the "right" criteria I want. These results would
then be the right side of the LEFT JOIN clause.
Any thoughts (other than I'm crazy )?!
Thanks!
Don