Re: Difficult select statement

Поиск
Список
Период
Сортировка
От Stephan Szabo
Тема Re: Difficult select statement
Дата
Msg-id 20030925102659.C82422@megazone.bigpanda.com
обсуждение исходный текст
Ответ на Difficult select statement  (Owen Funkhouser <funk@highwaay.net>)
Список 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 по дате отправления:

Предыдущее
От: Bruce Momjian
Дата:
Сообщение: Re: Group does not exist.
Следующее
От: Bruno Wolff III
Дата:
Сообщение: Re: Backing Up a Postgres database...