-- concat function (plpgsql) postgresql 8.0 CREATE FUNCTION concat (text, text) RETURNS text AS $$ DECLARE t text; BEGIN IF character_length($1) > 0 THEN t = $1 ||', '|| $2; ELSE t = $2; END IF; RETURN t; END; $$ LANGUAGE plpgsql; -- aggregate function pegar CREATE AGGREGATE pegar ( sfunc = concat, basetype = text, stype = text, initcond = '' ); -- usage example SELECT pais.pais, pegar(ciudad) FROM ciudad JOIN pais ON ciudad.idpais=pais.idpais GROUP BY pais.pais;