Forum OpenACS Q&A: Re: something like join of tcl for postgres?

Collapse
Posted by Jonathan Ellis on
there's no builtin, but you can create one thanks to postgresql's support for user-defined aggregate functions:
CREATE FUNCTION comma_cat(text, text) RETURNS text AS
'select case
WHEN $2 is null or $2 = '''' THEN $1
WHEN $1 is null or $1 = '''' THEN $2
ELSE $1 || '', '' || $2
END'
LANGUAGE 'sql';

CREATE AGGREGATE list(
    sfunc1=comma_cat,
    basetype=text,
    stype1=text,
    initcond1=''
);

bf2=# select list(foo) from (select 'a' as foo union select 'b' as foo) as q1;
 list
------
 a, b
(1 row)