Re: Help with join syntax sought

From: Andy Colson <andy(at)squeakycode(dot)net>
To: "James B(dot) Byrne" <byrnejb(at)harte-lyne(dot)ca>
Subject: Re: Help with join syntax sought
Date: 2009-05-19 21:02:35
Message-ID: 4A131E6B.5010407@squeakycode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message James B. Byrne 2009-05-19 21:09:51 Re: Help with join syntax sought
Previous Message Andy Colson 2009-05-19 20:41:07 Re: Help with join syntax sought