If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > NOT EXISTS problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-26-05, 10:43
steve_o steve_o is offline
Registered User
 
Join Date: Apr 2004
Posts: 43
NOT EXISTS problem

I have a table in which there are the following 2 columns: sitm and pitm.
sitm is the child item, and pitm is the parent item. i would like to find all child items that are not parent items.

I would have thought that the following query would do this:

Code:
select sitm from lhpms010
where not exists (select pitm from lhpms010)
Anybody have any suggestions why this doesn't work? There are certainly items in the sitm column that are not in the pitm column...

steven.
Reply With Quote
  #2 (permalink)  
Old 09-26-05, 10:45
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
How about

SELECT sitm FROM lhpms010
MINUS
SELECT pitm FROM lhpms010;
Reply With Quote
  #3 (permalink)  
Old 09-26-05, 15:44
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
If your DBMS does not support the minus, then you could try a correlated subquery with your not exists predicate.

Code:
select sitm from lhpms010 a
where not exists (select pitm from lhpms010 b
                         where b.pitm = a.sitm)
Reply With Quote
  #4 (permalink)  
Old 09-26-05, 23:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
there were two additional really good alternative approaches posted into the exact same thread as this one in the sql server forum

<sigh />
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-04-05, 09:51
Nirmala Nirmala is offline
Registered User
 
Join Date: Sep 2005
Posts: 20
Hi ,

The MINUS query should work.
Reply With Quote
  #6 (permalink)  
Old 10-04-05, 10:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Nirmala
The MINUS query should work.
but MINUS is proprietary, and this is the non-proprietary SQL forum
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 10-04-05, 10:57
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Try replacing MINUS by EXCEPT; that's standard SQL.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #8 (permalink)  
Old 10-04-05, 11:02
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by steve_o
select sitm from lhpms010
where not exists (select pitm from lhpms010)
Typically, in an EXISTS, the selected value (here pitm) is irrelevant.
So what you wanted was a WHERE condition in your subquery, specifying that the sitm of the outer query appears in the pitm column of the subquery:
Code:
select sitm from lhpms010 AS x
where not exists (select 1 from lhpms010 where pitm = x.sitm)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On