Example:
table_O
pfield = 667 cfield = 008 stat=C
pfield = 667 cfield = 307 stat=S
pfield = 777 cfield = 222 stat=S
pfield = 888 cfield = 111 stat=S
table_q
pfield = 667 cfield = 008
pfield = 777 cfield = 222
-- Step 1
-- I want all the pfield in table_O (w/ stat=S) that are NOT in table_q
select distinct (o.pfield) from table_O o, table_q q
where o.pfield = q.pfield (+) and q.pfield is null and o.stat = 'S'
Result:
pfield = 888
-- Step 2
-- I want the records from Step 1 and
-- include table_O (w/ stat=S) records that have a matching pfield in table_q, but not a matching cfield
SELECT o.pfield, o.cfield
FROM table_O o WHERE o.stat = 'S' and o.cfield not in (select distinct q.cfield from table_q q where q.pfield = o.pfield)
Result:
pfield = 667 cfield = 307
pfield = 888 cfield = 111
-- Step 3
-- I want the records in table_O (w/ stat=S) that have a matching pfield in table_q, but not a matching cfield
Desired result:
pfield = 667 cfield = 307
Question: Is there a single SQL query (without using a MINUS on the first 2 steps) that can give me the desired result in Step 3?
table_O
pfield = 667 cfield = 008 stat=C
pfield = 667 cfield = 307 stat=S
pfield = 777 cfield = 222 stat=S
pfield = 888 cfield = 111 stat=S
table_q
pfield = 667 cfield = 008
pfield = 777 cfield = 222
-- Step 1
-- I want all the pfield in table_O (w/ stat=S) that are NOT in table_q
select distinct (o.pfield) from table_O o, table_q q
where o.pfield = q.pfield (+) and q.pfield is null and o.stat = 'S'
Result:
pfield = 888
-- Step 2
-- I want the records from Step 1 and
-- include table_O (w/ stat=S) records that have a matching pfield in table_q, but not a matching cfield
SELECT o.pfield, o.cfield
FROM table_O o WHERE o.stat = 'S' and o.cfield not in (select distinct q.cfield from table_q q where q.pfield = o.pfield)
Result:
pfield = 667 cfield = 307
pfield = 888 cfield = 111
-- Step 3
-- I want the records in table_O (w/ stat=S) that have a matching pfield in table_q, but not a matching cfield
Desired result:
pfield = 667 cfield = 307
Question: Is there a single SQL query (without using a MINUS on the first 2 steps) that can give me the desired result in Step 3?