List Concatination

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: sqllist <pgsql-sql(at)postgresql(dot)org>
Subject: List Concatination
Date: 2001-03-09 05:17:27
Message-ID: 3AA86767.698DDFFF@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Folks,

I have an interesting problem. For purpose of presentation to users,
I'd like to concatinate a list of VARCHAR values from a subtable. To
simplify my actual situation:

CREATE TABLE clients (
client_id SERIAL PRIMARY KEY,
client_name VARCHAR(50) );

CREATE TABLE client_contacts (
contact_id SERIAL PRIMARY KEY,
client_id REFERENCES clients(client_id),
first_name VARCHAR(25),
last_name VARCHAR(25) );

CLients:
1 McMurphy Assoc.
2 Ross Construction

Contacts
1 1 Jim Henson
2 1 Pat O'Gannet
3 2 Sara Vaugn
3 2 Bill Murray

etc.

What I'd like to be able to do is present a list of clients and their
comma-seperated contacts in paragraph form, hence:

Client Contacts
McMurphy Assoc. Jim Henson, Pat O'Gannet, Kelley Moore

Ross Construction Sara Vaugn, Bill Murray, Peter Frump,
Siskel Ebert

I can figure out how to do this procedurally (using PL/pgSQL and a
cursor) but not how to do it declaratively (using only SQL). The reason
it's important to do it declaritively is that there are actually two
sub-tables with thousands of entries and the procedural approach is
rather slow.

Suggestions?

-Josh Berkus

--
______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message RbrtBrn3 2001-03-09 10:29:01 How does this query work.....?
Previous Message Ken Kline 2001-03-09 04:52:44 perl dbd