pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Walt Bigelow <walt@stimpy.com>
To: pgsql-sql@postgresql.org
To: walt@stimpy.com
Subject: subqueries
Date: Mon, 12 Apr 1999 11:58:52 -0700
Message-ID: <3712426C.8342F216@stimpy.com> (raw)

I am having difficulites geting the following query to run properly:

I have 1 table that is called tbladdress that keeps company address
information AND the company's contacts (employees) in the same table. 
The company has a type id of 1, and the contact has a type id of 2.  The
contact aslo has a field filled in which points to the agencyid field of
the parent company.  Not every company has a contact.

The query I was trying to get working:

SELECT agencyid, first, last 
FROM tbladdress
WHERE type = 1
AND agencyid NOT IN
	(SELECT distinct agencylink
	FROM tbladdress 
	WHERE type = 2)
ORDER BY last;

This query returns 0 rows.  I manually walked through the data and
found, yes, there are company records that should have been returned
(about 150 of them).

Just to test I did the following:

CREATE table aa (name varchar(50), number int4);
CREATE table bb (name varchar(50), number int4);

I populated those tables with:
aa: Mildred | 5
    Sam     | 6

bb: Paul    | 1
    Sativa  | 2

and the select:

SELECT * 
FROM aa 
WHERE number not in (select number from bb);

and I get the desired response:
name   |number
-------+------
Mildred|    5
Sam    |    6

What am I doing wrong?  Can the SQL query not do a nested select on a
table it is currently doing a select on?

Thanks for any insight!

Walt
walt@stimpy.com



view thread (6+ messages)  latest in thread

Message-ID: <3712426C.8342F216@stimpy.com>
Permalink:  ../3712426C.8342F216@stimpy.com/
Also on:    postgresql.org/message-id/3712426C.8342F216@stimpy.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: walt@stimpy.com
  Subject: Re: subqueries
  In-Reply-To: <3712426C.8342F216@stimpy.com>
  References: <3712426C.8342F216@stimpy.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox