Re: Difficult query

From: Manfred Koizar <mkoi-pg(at)aon(dot)at>
To: "Peter Alberer" <h9351252(at)obelix(dot)wu-wien(dot)ac(dot)at>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Difficult query
Date: 2003-03-06 10:38:30
Message-ID: 9q8e6v8qvq43ko5d07f56nkdjlqd5dl91a@4ax.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 5 Mar 2003 17:02:42 +0100, "Peter Alberer"
<h9351252(at)obelix(dot)wu-wien(dot)ac(dot)at> wrote:
>How can I filter the output to only return the top values per area? I
>would prefer not to use stored-procedures or client-side code if that is
>not necessary.

Peter,

DISTINCT ON is your friend. I never tried to use it together with
GROUP BY; at least you can use your original query as a sub-select
and wrap the DISTINCT ON query around it.

SELECT DISTINCT ON (area) area, user_id, total, succ
FROM (SELECT ...) AS t
ORDER BY area, succ DESC, total DESC;

Servus
Manfred

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Siva Kumar 2003-03-06 10:41:23 Re: foreign key constraint across databases
Previous Message Justin Clift 2003-03-06 08:24:42 Re: 7.3 rpms or not?