Обсуждение: sum of string columns, why ?

Поиск
Список
Период
Сортировка

sum of string columns, why ?

От
"Marcos Vaz - \( NewAge Group \)"
Дата:
I have one table with thwo columns, user and text, why sum all the text
fields of the one user ?

i need this.

thank you

Marcos



Re: sum of string columns, why ?

От
"Ross J. Reedstrom"
Дата:
On Wed, Jun 06, 2001 at 06:13:18PM -0300, Marcos Vaz - ( NewAge Group ) wrote:
> I have one table with thwo columns, user and text, why sum all the text
> fields of the one user ?

I assume you mean 'how' not 'why'?

You don't give a lot of detail, like what you mean by 'sum all the text'. One
interpretation is 'count the number of text entries for one user', as so:

select user, count(text) from some_table group by user;

If instead you mean concatenate all the text and return it as one big string,
that's a bit trickier. You need a custom aggregate function that concatenates
strings. Here's one I use to generate comma delimited concatenations:


REATE AGGREGATE catenate ( BASETYPE = text, SFUNC = commacat, STYPE = text, INITCOND = 
'' );

CREATE FUNCTION "commacat" (text,text) RETURNS text AS '
begin       if $1 <> '''' then               return $1 || '', '' || $2 ;       else               return $2;       end
if;
end;
' LANGUAGE 'plpgsql';

It works as so:

select user,catenate(text) from some_table group by user;

You'll want to change the helper function to not inserting commas, however.

Ross