Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select records where not in another table. 1

Status
Not open for further replies.

mrendino

MIS
Apr 3, 2002
2
US
This is probably easy but I'm just not figuring it out (obviously not a guru). I want to select all of the records in the newinventory table where newinventory.servicetag doesn't have a match in oldinventory.servicetag. Can someone please help?

Thanks, in advance.
 

Code:
select foo
  from newinventory N
left outer
  join oldinventory O
    on N.servicetag = O.servicetag
 where O.servicetag is null

alternatively,

Code:
select foo
  from newinventory N
 where not exists
       ( select servicetag 
           from oldinventory
          where servicetag = N.servicetag )

rudy
 
I think O.servicetag is never null. So this query never returns any rows.
Code:
select foo
  from newinventory N
left outer
  join oldinventory O
    on N.servicetag = O.servicetag
 where O.servicetag is null

But I think this will return rows where the new inventory is not in the old inventory.
Code:
select foo
  from newinventory N
left outer
  join (
         SELECT O.servicetag
         FROM newinventory N
         left outer join oldinventory O
         on N.servicetag = O.servicetag
        ) O2
    on N.servicetag = O2.servicetag
 where O2.servicetag is null

But I could be wrong.
 
Code:
select foo
  from newinventory N
left outer
  join oldinventory O
    on N.servicetag = O.servicetag
 where O.servicetag is null

>> "I think O.servicetag is never null.
>> So this query never returns any rows."

on the contrary, that is exactly what you need to look for

it's a left outer join, which means that the join produces all rows of N plus any matching rows of O -- but if, for a given N, there is no matching row of O, then the O columns in that row will be set to null, and when you test for this, then you get only unmatched rows

i think you're trying to do the same with the double left outer join, but it's more complex than necessary

rudy
 
Hello Rudy,

Right you are. What was I thinking about?

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top