X-Original-To: pgsql-sql@postgresql.org Received: from serwer.skawsoft.com.pl (serwer.skawsoft.com.pl [213.25.37.66]) by postgresql.org (Postfix) with ESMTP id 5B601476105 for ; Mon, 17 Mar 2003 14:05:43 -0500 (EST) Received: from klaster.net (ph24.krakow.cvx.ppp.tpnet.pl [217.99.208.24]) by serwer.skawsoft.com.pl (Postfix) with ESMTP id 2C52C6A1DE; Mon, 17 Mar 2003 20:05:38 +0100 (CET) Message-ID: <3E761CC1.6000906@klaster.net> Date: Mon, 17 Mar 2003 20:06:41 +0100 From: Tomasz Myrta User-Agent: Mozilla/5.0 (Windows; U; Win 9x 4.90; pl-PL; rv:1.3) Gecko/20030312 X-Accept-Language: pl, en-us, en MIME-Version: 1.0 To: "J.C.M. van der Kwast" Cc: pgsql-sql@postgresql.org Subject: Re: Howto?? References: <000801c2ec89$f70eeb60$04a779d9@cc299057a> In-Reply-To: <000801c2ec89$f70eeb60$04a779d9@cc299057a> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Archive-Number: 200303/412 X-Sequence-Number: 12451 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