Re: DISTINCT *and* ORDER BY in aggregate functions on expressions(!) - Mailing list pgsql-general

From Tom Lane
Subject Re: DISTINCT *and* ORDER BY in aggregate functions on expressions(!)
Date
Msg-id 532894.1677543065@sss.pgh.pa.us
Whole thread Raw
In response to DISTINCT *and* ORDER BY in aggregate functions on expressions(!)  (mirabilos <tg@evolvis.org>)
List pgsql-general
mirabilos <tg@evolvis.org> writes:
> This works well. However, what I seem to be not allowed to do is
> (without the extra COALESCE, to simplify):
> ...
> This is because, when I use DISTINCT (but only then‽), the ORDER BY
> arguments must be… arguments to the function, or something.

Well, yeah.  Simplify it to

  SELECT array_agg(DISTINCT x ORDER BY y) FROM mytable;

If there are several rows containing the same value of x and different
values of y, which y value are we supposed to sort the unique-ified x
value by?  It's an ill-defined query.

For the specific example you give, it's true that any specific
possible output of jsonb_build_object() would correspond to
a unique set of cot.weekday, cot.from_hour, cot.to_hour values.
But the parser can't be expected to know that.  Many functions
can produce the same output for different sets of inputs.

I'd suggest moving the distinct-ification into an earlier
processing step (i.e. a sub-select), or maybe thinking harder
about why you're getting duplicates in the first place.

            regards, tom lane



pgsql-general by date:

Previous
From: mirabilos
Date:
Subject: DISTINCT *and* ORDER BY in aggregate functions on expressions(!)
Next
From: "David G. Johnston"
Date:
Subject: Re: DISTINCT *and* ORDER BY in aggregate functions on expressions(!)