The following bug has been logged online:
Bug reference: 5563
Logged by: Daniel Grace
Email address: dgrace(at)wingsnw(dot)com
PostgreSQL version: 9.0beta3
Operating system: Windows XP 32-bit
Description: Odd behavior with aggregate_func(DISTINCT foo ORDER BY
foo)
Details:
The manual states:
"If DISTINCT is specified in addition to an order_by_clause, then all the
ORDER BY expressions must match regular arguments of the aggregate; that is,
you cannot sort on an expression that is not included in the DISTINCT list.
"
However, in some circumstances Postgres will fail
DROP TABLE IF EXISTS foo;
CREATE TABLE foo (
t VARCHAR
);
INSERT INTO foo (t) VALUES ('a'), ('a'), ('b'), ('b'), ('c');
SELECT STRING_AGG(DISTINCT t::TEXT ORDER BY t::TEXT) FROM foo;