Search Postgresql Archives

Re: Help with join syntax sought

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux