A
, A2Z
and Z
that I want to outer join from left to right:
The first table, A
, contains the (primary keys) 1 through 7. I want my select to return each of these, that's why I use an outer join.
A
's column i
is outer joined to A2Z
's column ia
:
A.i = A2Z.ia (+)
.
The (+)
symbol indicates that a record should be returned even if there is no matching record. Note, the (+)
is on the side of the =
where "missing" records are expected.
Now, the records in A2Z
should be joined to Z
if A2Z
's column flg
is equal to y
(colored green in the graphic above). For example, for the 1
in A
, I expected the query to return the a
in Z
, for the 2
in A
I expect no matching record in Z
since the corresponding flg
is either null
or not equal to y
.
This requirement can be implemented with a
A2Z.flg (+) = 'y'
Note, the (+)
is on the side where missing records (or null values) are expected. Since y
is neither missing nor null, it goes to the other side.
Finally, A2Z
needs to be joined to Z
:
A2Z.iz = Z.i (+)
Complete SQL Script
When run, the select returns the following records:
A_I Z
---------- -
1 a
2
3
4
5
6
7
No comments:
Post a Comment