Re: How do I concatenate row-wise instead of column-wise?

From: Richard Huxton <dev(at)archonet(dot)com>
To: Marcus Claesson <marcus(dot)claesson(at)angiogenetics(dot)se>, pgsql-sql(at)postgresql(dot)org
Subject: Re: How do I concatenate row-wise instead of column-wise?
Date: 2002-07-16 09:15:51
Message-ID: 200207161015.53342.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tuesday 16 Jul 2002 9:09 am, Marcus Claesson wrote:
> I have a table like this:
> SELECT * FROM old_tab;
> id | descr
> -------------------
> 3 | ddd
> 3 | ddd
> 3 | eee
> 3 | fff

> SELECT * FROM new_tab;
> id | descr
> --------------------------
> 1 | aaa;bbb
> 2 | ccc;bbb
> 3 | ddd;eee;fff
> 4 | bbb

You'll want to write your own aggregate function - something like max() which
will work over a range of values. This is easier than you might think.

The only thing to be careful of is that order isn't guaranteed, so by default
you could have "ddd;eee;fff" or "fff;ddd;eee" etc.

Go to techdocs.postgresql.org and check the "Postgresql Cookbook" courtesy of
Roberto Mello and also the "Postgresql Notes" by me. Also check the online
manual and the mailing archives (try searching on "aggregate" and "catenate"
or "concat").

HTH

- Richard Huxton

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Rajesh Kumar Mallah. 2002-07-16 11:25:14 Cascading deletions does not seem to work inside PL/PGSQL functions.
Previous Message Luis Alberto Amigo Navarro 2002-07-16 08:51:03 Re: [HACKERS] please help on query