Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> newbie : getting rid of IN clause
I have two tables :
table t1; fields : name old_qty new_qty
Entries : ('aa', 100, 115) and ('aa', 122, 222) table t2; fields name qty_change
Entries : (aa, 15) (aa, 100) (aa, 92)
I want to find entries in t2 whose qty_change is not 'reflected' in table t1.
Wrong query :
select t2.* from t2, t1 t1
where t1.part = t2.part and new_qty - old_qty <> qty_change
-- and not exists (
-- select null from t2 aa where t1.part = aa.part and new_qty - old_qty
= aa.qty_change)
-- )
(Even when the above 3 lines are uncommented, the
query is still wrong.) It's obvious why the query is wrong
but I somehow thought that a clause on the lines of
select ... FROM t2 t2, t1 aa, t1 bb where ...
(with t1 used twice in FROM list) would be the best
way to write the query.
Correct query :
select * from t2
where (part, qty_change)
not in (select part, new_qty - old_qty from t1
where t1.part = t2.part)
But does a better way to write the query exist?
(Both tables have a 'timestamp' field which serves as primary key.)
Please advise. TIA.
Received on Wed Aug 02 2006 - 15:26:04 CDT
![]() |
![]() |