Re: Difficult select statement - Mailing list pgsql-novice

From Stephan Szabo
Subject Re: Difficult select statement
Date
Msg-id 20030925102659.C82422@megazone.bigpanda.com
Whole thread Raw
In response to Difficult select statement  (Owen Funkhouser <funk@highwaay.net>)
List 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.

pgsql-novice by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Group does not exist.
Next
From: Bruno Wolff III
Date:
Subject: Re: Backing Up a Postgres database...