pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Manfred Koizar <mkoi-pg@aon.at>
To: Albrecht Berger <berger1517@gmx.ch>
Cc: pgsql <pgsql-sql@postgresql.org>
Subject: Re: sql group by statement
Date: Fri, 13 Sep 2002 14:16:02 +0200
Message-ID: <g2l3ou44jver0jvic4477cd66d0k92c6li@4ax.com> (raw)
In-Reply-To: <002f01c25b10$6db4b940$a07ba8c0@wxp>
References: <002f01c25b10$6db4b940$a07ba8c0@wxp>

On Fri, 13 Sep 2002 12:29:21 +0200, "Albrecht Berger"
<berger1517@gmx.ch> 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



view thread (6+ messages)  latest in thread

Message-ID: <g2l3ou44jver0jvic4477cd66d0k92c6li@4ax.com>
Permalink:  ../g2l3ou44jver0jvic4477cd66d0k92c6li@4ax.com/
Also on:    postgresql.org/message-id/g2l3ou44jver0jvic4477cd66d0k92c6li@4ax.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mkoi-pg@aon.at, berger1517@gmx.ch
  Subject: Re: sql group by statement
  In-Reply-To: <g2l3ou44jver0jvic4477cd66d0k92c6li@4ax.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox