Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 42E4C475B47 for ; Sat, 14 Sep 2002 11:03:54 -0400 (EDT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id 90EE2475ADE for ; Sat, 14 Sep 2002 11:03:53 -0400 (EDT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.5/8.12.5) with ESMTP id g8EF3T5D026756; Sat, 14 Sep 2002 11:03:33 -0400 (EDT) To: "Albrecht Berger" Cc: "pgsql" Subject: Re: sql group by statement In-reply-to: <006501c25b11$e30914b0$a07ba8c0@wxp> References: <002f01c25b10$6db4b940$a07ba8c0@wxp> <3D81BF17.4030609@mail.ru> <006501c25b11$e30914b0$a07ba8c0@wxp> Comments: In-reply-to "Albrecht Berger" message dated "Fri, 13 Sep 2002 12:39:46 +0200" Date: Sat, 14 Sep 2002 11:03:29 -0400 Message-ID: <26755.1032015809@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/150 X-Sequence-Number: 9427 "Albrecht Berger" writes: > but how do I know that "distinct on" doesn't cut off > the row with max(val2) of that id that I need ? Because you do SELECT DISTINCT ON (id) ... ORDER BY id, val2 DESC; The DISTINCT keeps the first of each group of rows with the same id, and by virtue of the ORDER BY (which acts first) the max val2 will be the first row in that group. Note there's no GROUP BY in this approach. regards, tom lane