Forum OpenACS Q&A: Response to SQL: Order by and Group by

Collapse
Posted by Don Baccus on
The other fields are aggregates (max, in this case; count's another common one).  The columns that are selected when "group by" is used must either appear in the "group by" or in a call to an aggregate function.

This makes a lot of sense if you consider how "group by" works.  If you select a column that's not in the "group by", and there's more than one row in that grow, which column value should be returned?  The  aggregate's OK because it's defined to operate on all of the rows within a particular group, returning a single value.