Re: Why does "group by" need to match select fields?

Поиск
Список
Период
Сортировка
От Bruno Wolff III
Тема Re: Why does "group by" need to match select fields?
Дата
Msg-id 20070302220843.GA28490@wolff.to
обсуждение исходный текст
Ответ на Why does "group by" need to match select fields?  (Omar Eljumaily <omar2@omnicode.com>)
Список pgsql-general
On Wed, Feb 28, 2007 at 16:19:02 -0800,
  Omar Eljumaily <omar2@omnicode.com> wrote:
> select max(amount), payee, id from checks group by payee;
>
> Why won't the above work?  Is there another way to get the id for the
> record with the highest amount for each payee?

While the DISTINCT ON approach is probably best if you can live with a
Postgres specific solution, the general way to do this is use the group by
query to get a set of primary keys with aggregates and then you join this
back to the original table to get the other data.

Some databases will also recognize that you are grouping by a candidate
key and allow you to specify normal columns since they must all have the
same value for rows with the same candidate key value. Unfortunately Postgres
doesn't do that now.

В списке pgsql-general по дате отправления:

Предыдущее
От: Bill Moran
Дата:
Сообщение: Re: I'd love to know what the rest of this error message is.
Следующее
От: Bruce Momjian
Дата:
Сообщение: Re: ftell mismatch with expected position