Re: pg SQL question - Mailing list pgsql-general

From Richard Poole
Subject Re: pg SQL question
Date
Msg-id 20050122213601.GB4160@guests.deus.net
Whole thread Raw
In response to pg SQL question  ("Ed L." <pgsql@bluepolka.net>)
List pgsql-general
On Sat, Jan 22, 2005 at 02:03:58PM -0700, Ed L. wrote:
>
> There's probably an obvious answer for this, but I couldn't see it in the
> docs.  What's the simplest way to concatenate multiple same-column values
> in SQL?

You can create an aggregate that does nothing but concatenate the entries:

CREATE AGGREGATE concat (
    BASETYPE = TEXT,
    SFUNC = textcat,
    STYPE = TEXT,
    INITCOND = ''
);

This uses the "textcat" function, which is already lurking in Postgres to
implement the || operator. Then you can go:

SELECT concat(entry) FROM (
    SELECT * FROM speech ORDER BY id
) AS lines;

And it will do what you want. The subselect with the ORDER BY guarantees
that the lines come out in the order you put them in.


Richard

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: SCHEMA compatibility with Oracle/DB2/Firebird
Next
From: Jarkko Elfving
Date:
Subject: About PostgreSQL 8.0