Unix Technical Forum

Concatenate rows

This is a discussion on Concatenate rows within the pgsql Novice forums, part of the PostgreSQL category; --> Hello, If I have a table like : ID COLOR 1 blue 2 red 3 green Could I write ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 08:31 PM
A Gilmore
 
Posts: n/a
Default Concatenate rows

Hello,

If I have a table like :

ID COLOR
1 blue
2 red
3 green

Could I write a SQL query that would return a single concatenated row
for all matching rows, with a specified seperator. For example like so :

blue:red:green:

Thank you for your time,
A Gilmore

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 08:31 PM
Mike G.
 
Posts: n/a
Default Re: Concatenate rows

I suppose you could create a function, use a cursor to walk through the table row by row, assign the result of the each row fetched to the same variable each time.

CREATE FUNCTION returns text
DECLARE
a text;
single_row text;

BEGIN

FETCH CURSOR INTO

a := SELECT COLOR FROM X;

single_row := single_row || ':' || a;

LOOP;

END;

return single_row;


On Sun, Feb 20, 2005 at 05:32:48PM -0800, A Gilmore wrote:
> Hello,
>
> If I have a table like :
>
> ID COLOR
> 1 blue
> 2 red
> 3 green
>
> Could I write a SQL query that would return a single concatenated row
> for all matching rows, with a specified seperator. For example like so :
>
> blue:red:green:
>
> Thank you for your time,
> A Gilmore
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 08:31 PM
Michael Fuhr
 
Posts: n/a
Default Re: Concatenate rows

On Sun, Feb 20, 2005 at 05:32:48PM -0800, A Gilmore wrote:

> If I have a table like :
>
> ID COLOR
> 1 blue
> 2 red
> 3 green
>
> Could I write a SQL query that would return a single concatenated row
> for all matching rows, with a specified seperator. For example like so :
>
> blue:red:green:


In PostgreSQL 7.4 and later you could do this:

SELECT array_to_string(array(SELECT color FROM foo), ':');

See "Array Constructors", "Arrays", and "Array Functions and
Operators" in the documentation. Here are links for the latest
version of PostgreSQL:

http://www.postgresql.org/docs/8.0/s...Y-CONSTRUCTORS
http://www.postgresql.org/docs/8.0/static/arrays.html
http://www.postgresql.org/docs/8.0/s...ons-array.html

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-17-2008, 08:31 PM
A Gilmore
 
Posts: n/a
Default Re: Concatenate rows

Michael Fuhr wrote:
> On Sun, Feb 20, 2005 at 05:32:48PM -0800, A Gilmore wrote:
>
>
>>If I have a table like :
>>
>>ID COLOR
>>1 blue
>>2 red
>>3 green
>>
>>Could I write a SQL query that would return a single concatenated row
>>for all matching rows, with a specified seperator. For example like so :
>>
>>blue:red:green:

>
>
> In PostgreSQL 7.4 and later you could do this:
>
> SELECT array_to_string(array(SELECT color FROM foo), ':');
>
> See "Array Constructors", "Arrays", and "Array Functions and
> Operators" in the documentation. Here are links for the latest
> version of PostgreSQL:
>
> http://www.postgresql.org/docs/8.0/s...Y-CONSTRUCTORS
> http://www.postgresql.org/docs/8.0/static/arrays.html
> http://www.postgresql.org/docs/8.0/s...ons-array.html
>


That works great, thank you very much. I had been reading over the much
documentation but mistakenly skipped arrays assuming it wouldn't be
applicable.

A Gilmore

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:36 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com