pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomasz Myrta <jasiek@klaster.net>
To: J.C.M. van der Kwast <jkwast@home.nl>
Cc: pgsql-sql@postgresql.org
Subject: Re: Howto??
Date: Mon, 17 Mar 2003 20:06:41 +0100
Message-ID: <3E761CC1.6000906@klaster.net> (raw)
In-Reply-To: <000801c2ec89$f70eeb60$04a779d9@cc299057a>
References: <000801c2ec89$f70eeb60$04a779d9@cc299057a>

Uz.ytkownik J.C.M. van der Kwast napisa?:
> Hi,
>  
> After trying for a week now I've come to the conclusion that help is 
> needed. I've got the following problem:
> I have a table with id column, another id (id2) column and a value 
> column. For each id there can be more id2. I want id2 for id where the 
> value is min. I tried several things with having but it doesn't produce 
> what I need. Example:
>  
> select id, id2, min(value) from table
> group by id
> having count (*) >1 and min(value) = (select min(value) from table)
>  
> Now it complains about id2 not being part of aggregate.  
> In order to get the min value I have to group by id, right? But because 
> id2 is unique it won't do min on those values, right? So how do you 
> tackle a problem like that? Any suggestions?
>  
> thanks sjors

select distinct on(id) id2 from table
order by id,value;

Regards,
Tomasz Myrta




view thread (5+ messages)

Message-ID: <3E761CC1.6000906@klaster.net>
Permalink:  ../3E761CC1.6000906@klaster.net/
Also on:    postgresql.org/message-id/3E761CC1.6000906@klaster.net

 · 

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: jasiek@klaster.net, jkwast@home.nl
  Subject: Re: Howto??
  In-Reply-To: <3E761CC1.6000906@klaster.net>

* 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