Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 3B734475F5E for ; Fri, 13 Sep 2002 06:28:36 -0400 (EDT) Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by postgresql.org (Postfix) with SMTP id 05315475F38 for ; Fri, 13 Sep 2002 06:28:35 -0400 (EDT) Received: (qmail 10245 invoked by uid 0); 13 Sep 2002 10:28:36 -0000 Received: from pd9e6d96a.dip.t-dialin.net (HELO wxp) (217.230.217.106) by mail.gmx.net (mp011-rz3) with SMTP; 13 Sep 2002 10:28:36 -0000 Message-ID: <002f01c25b10$6db4b940$a07ba8c0@wxp> From: "Albrecht Berger" To: "pgsql" Subject: sql group by statement Date: Fri, 13 Sep 2002 12:29:21 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/128 X-Sequence-Number: 9405 Hello, I have a problem, which I'm not able to solve with a simple query : I need a resultset with distinct id's, but the max val2 of each id. I tried to group by id, but though I need the pk in my resultset I have to group it too, which "destroys" the group of val2. Can this be done without a huge query ? 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 Thx berger