Re: Suggestion. Optional local ORDER BY clause for DISTINCT ON

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Stefan Stefanov <stefanov(dot)sm(at)abv(dot)bg>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Suggestion. Optional local ORDER BY clause for DISTINCT ON
Date: 2023-10-10 18:29:23
Message-ID: 432142.1696962563@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Stefan Stefanov <stefanov(dot)sm(at)abv(dot)bg> writes:
> Gents, I have a suggestion for DISTINCT ON clause syntax.
> DISTINCT ON (expression(s) [ORDER BY expression(s)])
> Determines the precedence within each DISTINCT ON group (i.e. the ‘first’ row to be picked)

> Motivation
> • Using the query-wide ORDER BY clause to determine which record to pick mixes two unrelated concerns, ‘first’ row selection and result-set ordering. This may be confusing;
> • The DISTINCT ON expression(s) must match the leftmost ORDER BY expression(s). This may cause inconvenience and require nesting as a sub-query to order the result-set.

Since you can get the desired behavior with a sub-select, I'm
not especially excited about extending DISTINCT ON. If it weren't
such a nonstandard kluge, I might feel differently; but it's not
an area that I think we ought to put more effort into.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2023-10-10 18:43:34 Re: New WAL record to detect the checkpoint redo location
Previous Message Tom Lane 2023-10-10 18:24:08 Re: Check each of base restriction clauses for constant-FALSE-or-NULL