Suggestion. Optional local ORDER BY clause for DISTINCT ON

From: Stefan Stefanov <stefanov(dot)sm(at)abv(dot)bg>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Suggestion. Optional local ORDER BY clause for DISTINCT ON
Date: 2023-10-10 17:21:30
Message-ID: 2061495418.2763075.1696958490409@nm33.abv.bg
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.

Pros
• Backward compatibility. If the local ORDER BY clause is missing then the current rules shall apply;
• Familiar and consistent syntax and semantics, the same as in *_agg functions;
• Clear distinction of first row selection and result-set ordering;
• Good readability;
• The DISTINCT ON expression(s) do not have to match the leftmost ORDER BY expression(s).

Cons
• Possible extra verbosity
Best regards, Stefan
1 1 1 1 MicrosoftInternetExplorer4 0 2 DocumentNotSpecified 7.8 磅 Normal 0

































































































































































































































































Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2023-10-10 17:56:16 Re: Check each of base restriction clauses for constant-FALSE-or-NULL
Previous Message Tom Lane 2023-10-10 17:13:48 Re: Retire has_multiple_baserels()