Re: Functions in sort order - undocumented restriction - Mailing list pgsql-docs

From Tom Lane
Subject Re: Functions in sort order - undocumented restriction
Date
Msg-id 5183.1518288997@sss.pgh.pa.us
Whole thread Raw
In response to Functions in sort order - undocumented restriction  (PG Doc comments form <noreply@postgresql.org>)
List pgsql-docs
=?utf-8?q?PG_Doc_comments_form?= <noreply@postgresql.org> writes:
> This does not work:

>  select distinct (o.bid), organisation, posttown, replace(case when postcode
> ='' then null else trim(postcode) end, ' ', '') as pc, phone, o.active,
> website, email, (select count(*) from notes as n where n.bid = o.bid) as nn
> from organisations as o right join notes as n on o.bid = n.bid where true
> order by replace(case when phone ='' then null else trim(phone) end, ' ',
> '') nulls last ;
> ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list

What do you find unclear about this error message?

The reason seems clear enough to me: if the ordering expression isn't one
of the values that are being de-duplicated on, then there isn't a unique
value to associate with each surviving row for sorting purposes.

            regards, tom lane


pgsql-docs by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: Functions in sort order - undocumented restriction
Next
From: Tom Lane
Date:
Subject: Re: Documentation of EXCEPT ALL may have a bug