Re: Help with join syntax sought - Mailing list pgsql-general

From Andy Colson
Subject Re: Help with join syntax sought
Date
Msg-id 4A131E6B.5010407@squeakycode.net
Whole thread Raw
In response to Help with join syntax sought  ("James B. Byrne" <byrnejb@harte-lyne.ca>)
Responses Re: Help with join syntax sought
List 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 by date:

Previous
From: Andy Colson
Date:
Subject: Re: origins/destinations
Next
From: Andy Colson
Date:
Subject: Re: Help with join syntax sought