Re: Difficult select statement

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Owen Funkhouser <funk(at)highwaay(dot)net>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Difficult select statement
Date: 2003-09-25 17:29:42
Message-ID: 20030925102659.C82422@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Wed, 24 Sep 2003, Owen Funkhouser wrote:

> I have two tables.
>
> Table "data" has three fields, "id integer", "project text", and "component text".
>
> Table "participants" has two fields, "id integer" and "username text".
>
> The id in each table correspond to the same thing.
>
> The values for each table is as follows:
>
> data
> ----
> 1 | Stocks | Decisions
> 2 | Stocks | Loss
> 3 | Stocks | Profits
> 4 | Bonds | Interest
>
> participants
> ------------
> 1 | George
> 1 | Harry
> 1 | Carmen
> 2 | Owen
> 2 | John
> 3 | Harry
> 4 | Bubba
>
> What I want is a distinct list of id, project, and component where username = Harry.
> Resulting in the following:
>
> 1 | Stocks | Decisions
> 3 | Stocks | Profits

I'd think something like:

select distinct id, project, component from data inner join participants
using (id) where username='Harry';

will give you what you want if I'm understanding the request correctly.
Your example data doesn't seem to have any cases where the distinct
matters, but I assume the real data does. If it doesn't (like there's a
unique constraint on data.id and a two column unique constraint on id,
username for participants), you don't need the distinct in the query.

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Derrick Betts 2003-09-25 17:44:01 Fw: Difficult select statement
Previous Message Bruce Momjian 2003-09-25 17:25:15 Re: Group does not exist.