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