Re: select min row in a group

Поиск
Список
Период
Сортировка
От Manfred Koizar
Тема Re: select min row in a group
Дата
Msg-id 48ophu8t7k4rsklf3go4fp69hmkqoslptp@4ax.com
обсуждение исходный текст
Ответ на select min row in a group  (Gregory Seidman <gss+pg@cs.brown.edu>)
Ответы Re: select min row in a group  (Gregory Seidman <gss+pg@cs.brown.edu>)
Список pgsql-general
On Fri, 28 Jun 2002 17:56:17 -0400, Gregory Seidman
<gss+pg@cs.brown.edu> wrote:
>This is really an SQL question, not specific to PostgreSQL.
Greg,

but the answer is specific to PostgreSQL :-)

>For each ext_id (which references something in another table), I want to
>find the row with the minimum value.
>I feel like there has to be a way to do it without a nested query, probably
>using GROUP BY. Any thoughts?

If you only need ext_id and value, this should work with most SQL
dialects:

    SELECT ext_id, min(value)
    FROM ValTable
    GROUP BY ext_id;

If you need all the other columns, use this PostgreSQL extension:

    SELECT DISTINCT ON (ext_id) ext_id, value, ...
    FROM ValTable
    ORDER BY ext_id, value;

Servus
 Manfred



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

Предыдущее
От: Gregory Seidman
Дата:
Сообщение: Re: serial columns & loads misfeature?
Следующее
От: Manfred Koizar
Дата:
Сообщение: Re: serial columns & loads misfeature?