Re: sum of string columns, why ?

Поиск
Список
Период
Сортировка
От Ross J. Reedstrom
Тема Re: sum of string columns, why ?
Дата
Msg-id 20010618114137.J14955@rice.edu
обсуждение исходный текст
Ответ на sum of string columns, why ?  ("Marcos Vaz - \( NewAge Group \)" <marcos.vaz@newage-software.com.br>)
Список pgsql-sql
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


В списке pgsql-sql по дате отправления:

Предыдущее
От: Roberto Mello
Дата:
Сообщение: Re: Better Archives?
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Better Archives?