Re: picking max from list

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Jodi Kanter <jkanter(at)virginia(dot)edu>
Cc: Postgres SQL List <pgsql-sql(at)postgresql(dot)org>
Subject: Re: picking max from list
Date: 2003-12-10 20:53:40
Message-ID: 20031210205340.GA18726@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, Dec 10, 2003 at 15:37:10 -0500,
Jodi Kanter <jkanter(at)virginia(dot)edu> wrote:
> I have a query that produces results similar to this:
>
> run# rd_pk group#
> 0 9209 5
> 1 9209 8
> 0 9520 2
> 1 9520 5
> 0 9520 etc....
> 0 8652
> 1 8652
> 2 8652
> 0 8895
> 1 8894
>
> Ultimately I want to know the group number for EACH rd_pk with the
> highest run number. Can this be done in one query? Or will I need to
> code with a loop?
> Thanks

You can do this using the postgres distinct on extension.
The query would look something like:
select distinct on (rd_pk) run, rd_pk, group from table_name
order by run desc;

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Guy Fraser 2003-12-10 23:11:10 Re: picking max from list
Previous Message Jodi Kanter 2003-12-10 20:37:10 picking max from list