Re: sql group by statement - Mailing list pgsql-sql

From Tom Lane
Subject Re: sql group by statement
Date
Msg-id 26755.1032015809@sss.pgh.pa.us
Whole thread Raw
In response to Re: sql group by statement  ("Albrecht Berger" <berger1517@gmx.ch>)
List pgsql-sql
"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


pgsql-sql by date:

Previous
From: Christopher Kings-Lynne
Date:
Subject: Re: Table alias in DELETE statements
Next
From: "Florian Mader"
Date:
Subject: Re: Timestamp Fractions Problem