Re: LISTAGG à la Oracle in PostgreSQL

From: Paul A Jungwirth <pj(at)illuminatedcomputing(dot)com>
To: Pierre Forstmann <pierre(dot)forstmann(at)gmail(dot)com>
Cc: "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: LISTAGG à la Oracle in PostgreSQL
Date: 2026-03-09 22:05:17
Message-ID: CA+renyUW6+JUM5d6OJ8XZR3araY2iii5xTYP5kSUjqgLdY+uSg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Mar 9, 2026 at 1:21 PM Pierre Forstmann
<pierre(dot)forstmann(at)gmail(dot)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(at)illuminatedcomputing(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Juan Rodrigo Alejandro Burgos Mella 2026-03-10 08:45:27 Re: LISTAGG à la Oracle in PostgreSQL
Previous Message Pierre Forstmann 2026-03-09 20:21:44 LISTAGG à la Oracle in PostgreSQL