BUG #18757: string_agg function(text, text) design issue

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: shencangsheng(at)126(dot)com
Subject: BUG #18757: string_agg function(text, text) design issue
Date: 2024-12-30 06:22:11
Message-ID: 18757-162f6938197913ac@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 18757
Logged by: CangSheng Shen
Email address: shencangsheng(at)126(dot)com
PostgreSQL version: 13.9
Operating system: MacOS
Description:

string_agg function(text, text) design issue

```sql
create table users (
id SERIAL PRIMARY KEY,
name character varying(100),
no integer
);

select
name,
string_agg(no::text, ',' order by no asc) as nos
from
users
group by name;
```

When I require distinct, I cannot continue using no for sorting; instead, I
need to use no::text, otherwise I will encounter the exception: `in an
aggregate with DISTINCT, ORDER BY expressions must appear in argument
list.`

```sql
select
name,
string_agg(no::text, ',' order by no::text asc) as nos
from
users
group by name;
```

This results in incorrect sorting and does not meet expectations.

Could you optimize `string_agg` or suggest an alternative function to handle
such a scenario?

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2024-12-30 16:03:58 BUG #18758: Incorrect query result caused by ROLLUP operation
Previous Message Tomas Vondra 2024-12-30 02:12:56 Re: pg_upgrade cannot create btrfs clones on linux kernel 6.8.0