public inbox for [email protected]  
help / color / mirror / Atom feed
From: Paul A Jungwirth <[email protected]>
To: Pierre Forstmann <[email protected]>
Cc: [email protected] <[email protected]>
Subject: Re: LISTAGG à la Oracle in PostgreSQL
Date: Mon, 9 Mar 2026 15:05:17 -0700
Message-ID: <CA+renyUW6+JUM5d6OJ8XZR3araY2iii5xTYP5kSUjqgLdY+uSg@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>

On Mon, Mar 9, 2026 at 1:21 PM Pierre Forstmann
<[email protected]> 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              ~{:-)
[email protected]






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: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: LISTAGG à la Oracle in PostgreSQL
  In-Reply-To: <CA+renyUW6+JUM5d6OJ8XZR3araY2iii5xTYP5kSUjqgLdY+uSg@mail.gmail.com>

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

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