pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: shencangsheng@126.com
Subject: BUG #18757: string_agg function(text, text) design issue
Date: Mon, 30 Dec 2024 06:22:11 +0000
Message-ID: <18757-162f6938197913ac@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      18757
Logged by:          CangSheng Shen
Email address:      shencangsheng@126.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?



view thread (2+ messages)  latest in thread

Message-ID: <18757-162f6938197913ac@postgresql.org>
Permalink:  ../18757-162f6938197913ac@postgresql.org/
Also on:    postgresql.org/message-id/18757-162f6938197913ac@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, pgsql-bugs@lists.postgresql.org, shencangsheng@126.com
  Subject: Re: BUG #18757: string_agg function(text, text) design issue
  In-Reply-To: <18757-162f6938197913ac@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox