How to collect text-fields from multiple rows ?

From: Andreas <maps(dot)on(at)gmx(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: How to collect text-fields from multiple rows ?
Date: 2010-10-16 03:23:26
Message-ID: 4CB91AAE.6030602@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,

how can I collect text-fields from multiple rows into one output row?
I'd like to do an equivalent to the aggregate function SUM() only for text.

The input is a select that shows among other things a numerical column
where I would like to group by.
The text column of all rows in a group should get concatenated into 1
text devided by a '\n'.

Even better would be if I could add a second text colum per line as topic.

Input e.g.
select group_nr::integer, memo::text, topic::text ...

1, 'bla ', 'weather'
2, 'yada..', 'weather'
2, 'talk talk..', 'cooking'
2, 'words words, ...', 'poetry'
3, ....

Output:

1, 'weather\nbla...'
2, 'weather\nyada..\ncooking\ntalk talk..\npoetry\nwords words, ...'

Even better would be to get some kind of line-chars as optical devider
between the topics.
Lets say 10 = above all topic-lines.

2, '==========\nweather\nyada..\n==========\ncooking\ntalk
talk..\n==========\npoetry\nwords words, ...'

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Thomas Kellerer 2010-10-16 07:23:03 Re: How to collect text-fields from multiple rows ?
Previous Message James Cloos 2010-10-15 09:57:23 Re: counting related rows