Re: Help with join syntax sought

Поиск
Список
Период
Сортировка
От Andy Colson
Тема Re: Help with join syntax sought
Дата
Msg-id 4A131E6B.5010407@squeakycode.net
обсуждение исходный текст
Ответ на Help with join syntax sought  ("James B. Byrne" <byrnejb@harte-lyne.ca>)
Ответы Re: Help with join syntax sought  ("James B. Byrne" <byrnejb@harte-lyne.ca>)
Список pgsql-general
James B. Byrne wrote:
> I am perplexed why I cannot select a column from the table without
> having to include it in the GROUP BY clause as well.
>
> Any help is welcomed.
>

Group by is saying "I want only one row returned for each distinct value
in this column"

so a food table like this:

name  |  type
--------------
apple | fruit
pie   | desert
orange| fruit

if you: select name, type from food group by type

your saying, give me only one row for each "type"... but there are two
records where type = 'fruit', so how do you return two values (apple,
orange) in only one row?

That's why all fields in the select list must be an aggregate function,
or in the group by  list.

so: select max(name), type from food group by type
works cuz we only get one name (the max name) back for each type.

or: select name, type from food group by type, name
which in our example is kinda pointless, but still, give us the distinct
items for "type, name".

-Andy

В списке pgsql-general по дате отправления:

Предыдущее
От: Andy Colson
Дата:
Сообщение: Re: origins/destinations
Следующее
От: Andy Colson
Дата:
Сообщение: Re: Help with join syntax sought