From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Thorsten Glaser <tg(at)evolvis(dot)org> |
Cc: | pgsql-general(at)lists(dot)postgresql(dot)org |
Subject: | Re: DISTINCT *and* ORDER BY in aggregate functions on expressions(!) |
Date: | 2023-02-28 01:32:58 |
Message-ID: | 559416.1677547978@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Thorsten Glaser <tg(at)evolvis(dot)org> writes:
> On Mon, 27 Feb 2023, Tom Lane wrote:
>> Well, yeah. Simplify it to
>> SELECT array_agg(DISTINCT x ORDER BY y) FROM mytable;
> That’s… a bit too simple for this case.
Sure, I was just trying to explain the rule.
>> 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.
> Not necessarily (see directly below), but why would that matter?
> It should sort the generated JSON objects within the array.
Well, that may be what you want, but it's not what you wrote in
the query. Follow David's advice and do
jsonb_agg(DISTINCT jsonb_build_object(
'weekday', cot.weekday,
'from_hour', cot.from_hour,
'to_hour', cot.to_hour)
ORDER BY jsonb_build_object(
'weekday', cot.weekday,
'from_hour', cot.from_hour,
'to_hour', cot.to_hour))
I'm pretty sure that this will only incur one evaluation of the
common subexpression, so even though it's tedious to type it's not
inefficient.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | David G. Johnston | 2023-02-28 01:48:42 | Re: DISTINCT *and* ORDER BY in aggregate functions on expressions(!) |
Previous Message | Thorsten Glaser | 2023-02-28 01:22:47 | Re: DISTINCT *and* ORDER BY in aggregate functions on expressions(!) |