Re: sql group by statement

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Albrecht Berger" <berger1517(at)gmx(dot)ch>
Cc: "pgsql" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: sql group by statement
Date: 2002-09-14 15:03:29
Message-ID: 26755.1032015809@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Albrecht Berger" <berger1517(at)gmx(dot)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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message shahbaz 2002-09-14 22:53:24 cross tab (pivtor table) sql
Previous Message Christopher Kings-Lynne 2002-09-14 06:07:46 Re: Table alias in DELETE statements