Re: A simple join question that may stump you

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: nospam4(at)pobox(dot)com (Ross Smith), pgsql-sql(at)postgresql(dot)org
Subject: Re: A simple join question that may stump you
Date: 2001-09-27 15:32:54
Message-ID: web-126749@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Ross,

> ID FLAG
> ----- -----
> 1 1
> 2 1
> 2 2
> 3 1
> 3 2
> 3 3
>
> and table B:
>
> FLAG
> -----
> 1
> 2
>
> I want to find all id's from table A that have every flag in table B
> but no extra flags. So, I'd end up with:
>
> ID
> -----
> 2

Try looking at the INTERSECT and EXCEPT join types. Thus, in pseudo-sql
you'd need:

SELECT A.ID
WHERE Count A JOIN B = Count B
AND Count A EXCEPT B = 0

Assuming that all rows in A are unique.

-Josh

______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

Attachment Content-Type Size
unknown_filename text/plain 2 bytes
unknown_filename text/plain 2 bytes
unknown_filename text/plain 2 bytes

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2001-09-27 16:25:50 Re: PARSER ERROR persists ....
Previous Message Tom Lane 2001-09-27 15:26:37 Re: HP-UX 11.0 postgres compile error!