From: Tom Lane <tgl@sss.pgh.pa.us>
To: Albrecht Berger <berger1517@gmx.ch>
Cc: pgsql <pgsql-sql@postgresql.org>
Subject: Re: sql group by statement
Date: Sat, 14 Sep 2002 11:03:29 -0400
Message-ID: <26755.1032015809@sss.pgh.pa.us> (raw)
In-Reply-To: <006501c25b11$e30914b0$a07ba8c0@wxp>
References: <002f01c25b10$6db4b940$a07ba8c0@wxp>
<3D81BF17.4030609@mail.ru>
<006501c25b11$e30914b0$a07ba8c0@wxp>
"Albrecht Berger" <berger1517@gmx.ch> 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
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: tgl@sss.pgh.pa.us, berger1517@gmx.ch
Subject: Re: sql group by statement
In-Reply-To: <26755.1032015809@sss.pgh.pa.us>
* 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