From: Richard Huxton <dev@archonet.com>
To: pgsql-sql@postgresql.org
To: juerg.rietmann@pup.ch
Subject: Re: Still don't know how to build this string ?
Date: Mon, 26 Mar 2001 13:23:15 +0100
Message-ID: <006501c0b5ef$897f33c0$1001a8c0@archonet.com> (raw)
References: <OFB356DA1B.8A1E6DD6-ONC1256A1B.0037F528@pup.ch>
From: <juerg.rietmann@pup.ch>
> I have a table with diameters and types. I need to build a comma separated
> string.
>
> typ diam
> 01 800
[snip]
>
> select diam from zylinder where typ='01'
>
> should produce the string "800,840,870,1120"
Try the following as a starting point:
CREATE FUNCTION comma_join(text,text) RETURNS text AS '
BEGIN
IF $1>\'\' AND $2>\'\' THEN
RETURN $1 || \',\' || $2;
ELSE
RETURN $1 || $2;
END IF;
END;
' LANGUAGE 'plpgsql';
CREATE AGGREGATE joinall (
sfunc = comma_join,
basetype = text,
stype = text,
initcond = ''
);
\d dia
Table "dia"
Attribute | Type | Modifier
-----------+---------+----------
typ | integer |
diam | integer |
SELECT typ,joinall(diam::text) FROM dia GROUP BY typ;
typ | joinall
-----+----------------------------
1 | 800,840,870,1120
2 | 760,800,900,1200,1234,1352
(2 rows)
Note the explicit cast of diam into text.
- Richard Huxton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: dev@archonet.com, juerg.rietmann@pup.ch
Subject: Re: Still don't know how to build this string ?
In-Reply-To: <006501c0b5ef$897f33c0$1001a8c0@archonet.com>
References: <OFB356DA1B.8A1E6DD6-ONC1256A1B.0037F528@pup.ch> <006501c0b5ef$897f33c0$1001a8c0@archonet.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox