LISTAGG à la Oracle in PostgreSQL

From: Pierre Forstmann <pierre(dot)forstmann(at)gmail(dot)com>
To: "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: LISTAGG à la Oracle in PostgreSQL
Date: 2026-03-09 20:21:44
Message-ID: de6c5d45-7ce9-42bd-9528-97fdf4ffa0e4@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

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 failed and IA also failed. Claude says:

It is not possible to exactly replicate listagg(ename, ',') WITHIN GROUP
(ORDER BY ename) as a custom PostgreSQL aggregate
because PostgreSQL strictly forbids ungrouped columns as direct
arguments to ordered-set aggregates.

Do you agree ?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Paul A Jungwirth 2026-03-09 22:05:17 Re: LISTAGG à la Oracle in PostgreSQL
Previous Message Adrian Klaver 2026-03-09 19:52:59 Re: Index (primary key) corrupt?