I am dealing with tables which (for the purposes of displaying here) look like the following:
A
A_ID | Clob_Col
1 | value
2 | value
3 | null
4 | value
5 | null
6 | value
7 | value
8 | null
9 | value
10 | value
B
B_ID |A_ID | C_ID
10 | 1 | 20
11 | 2 | 20
12 | 6 | 21
13 | 7 | 22
14 | 8 | 22
15 | 9 | 23
C
C_ID
20
21
22
23
24
25
(All tables have more columns)
I wish to write a query which will return values from all three tables but exclude records where certain values match those in Table C (NOT IN). My problem is to return the values from Table A which do not link to Table B when using a NOT IN clause on Table C.
Example:
SELECT a.A_ID, a.Clob_Col, b.B_ID, c.C_ID
from A a
LEFT JOIN B b on a.A_ID=b.A_ID
LEFT JOIN C c on b.C_ID=c.C_ID
WHERE a.AID >= 2
AND a.AID <= 7
AND c.C_ID NOT IN (22, 23, 24)
The last line - c.C_ID NOT IN (22, 23, 24) - will leave us with the following records in the B table: b_BID = 10,11 or 12
In turn, these link to the following records in Table A: a.ID = 1,2 and 6.
The first clause - a.AID >= 2 AND a.AID <= 7 - further restricts our final result to a.ID = 2 and 6.
The final result will look like:
A_ID |Clob_Col |B_ID |C_ID
2 |value |11 |20
6 |value |12 |21
But I also want to return the A table records which don't link to Table B - a.ID 3, 4 and 5 So I expect my results to be
A_ID |Clob_Col |B_ID |C_ID
2 |value |11 |20
3 |null |null |null
4 |value |null |null
5 |null |null |null
6 |value |12 |21
Note: The reason I included the Clob value is that someone suggested selecting all records and then performing a MINUS operation on the records where c.C_ID IN (22, 23, 24).
This seemed reasonable however Oracle does not allow MINUS operation where Clob columns are involved.