"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