Re: LISTAGG à la Oracle in PostgreSQL - Mailing list pgsql-general

From Paul A Jungwirth
Subject Re: LISTAGG à la Oracle in PostgreSQL
Date
Msg-id CA+renyUW6+JUM5d6OJ8XZR3araY2iii5xTYP5kSUjqgLdY+uSg@mail.gmail.com
Whole thread
In response to LISTAGG à la Oracle in PostgreSQL  (Pierre Forstmann <pierre.forstmann@gmail.com>)
Responses Re: LISTAGG à la Oracle in PostgreSQL
List pgsql-general
On Mon, Mar 9, 2026 at 1:21 PM Pierre Forstmann
<pierre.forstmann@gmail.com> wrote:
>
> Hello,
>
> I can write a LISTAGG aggregate for:
>
> create table emp(deptno numeric, ename text);
>
> SELECT deptno, LISTAGG(ename, ','::text ORDER BY ename) AS employees
> FROM   emp GROUP BY deptno ORDER BY deptno;
>
> I would like to know if is possible to create an aggregate LISTAGG that
> would work like in Oracle:
>
> SELECT deptno,
>         listagg(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees
> FROM emp
> GROUP BY deptno
> ORDER BY deptno;

I don't think you need a custom aggregate here. In Postgres you can say:

select deptno,
      string_agg(ename, ',' ORDER BY ename) AS employees
FROM emp
GROUP BY deptno
ORDER BY deptno;

--
Paul              ~{:-)
pj@illuminatedcomputing.com



pgsql-general by date:

Previous
From: Pierre Forstmann
Date:
Subject: LISTAGG à la Oracle in PostgreSQL
Next
From: Juan Rodrigo Alejandro Burgos Mella
Date:
Subject: Re: LISTAGG à la Oracle in PostgreSQL