Re: a SQL query question

Поиск
Список
Период
Сортировка
От Klint Gore
Тема Re: a SQL query question
Дата
Msg-id 488E88E5.1010302@une.edu.au
обсуждение исходный текст
Ответ на a SQL query question  (Rajarshi Guha <rguha@indiana.edu>)
Список pgsql-general
Rajarshi Guha wrote:
> What I'm trying to do is to select those rows where pid is not null,
> grouped by pid.
>
>  From within each group I'd like to select the row that has the
> maximum value of nmol.
>
Distinct on should do the job for you.
   select distinct on (pid) aid, pid, nmol
   from atable
   where pid is not null
   order by pid, nmol desc

If you want the rows tie for max nmol within a pid then you can go to
   select aid,pid,nmol
   from atable
   where (pid,nmol) in (select pid, max(nmol) from atable where pid is
not null group by pid)

klint.

--
Klint Gore
Database Manager
Sheep CRC
A.G.B.U.
University of New England
Armidale NSW 2350

Ph: 02 6773 3789
Fax: 02 6773 3266
EMail: kgore4@une.edu.au


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

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: why can't I load pgxml.sql
Следующее
От: brian
Дата:
Сообщение: Re: a SQL query question