On Fri, 13 Sep 2002 12:29:21 +0200, "Albrecht Berger"
<[EMAIL PROTECTED]> wrote:
>Table :
>pk   id   val1 val2
> 1    1    2   3
> 2    1    2   4
> 3    2    1   1
> 4    1    0   5
> 5    2    1   8
> 
>
>Needed Result :
>pk   id   val1 val2
> 4    1    0   5
> 5    2    1   8

Albrecht,

"DISTINCT ON eliminates rows that match on all the specified
expressions, keeping only the first row of each set of duplicates."
So the trick is to sort appropriately:

        SELECT DISTINCT on (id) pk, id, val1, val2
          FROM yourtable
         ORDER BY id asc, val2 desc, pk desc;

Servus
 Manfred

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Reply via email to