LISTAGG à la Oracle in PostgreSQL
pierre.forstmann@gmail.com
From: Pierre Forstmann <pierre.forstmann@gmail.com>
To: "pgsql-general@lists.postgresql.org" <pgsql-general@lists.postgresql.org>
Date: 2026-03-09T20:21:44Z
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 ?