pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
sum of string columns, why ?
2+ messages / 2 participants
[nested] [flat]

* sum of string columns, why ?
@ 2001-06-06 21:13  Marcos Vaz - ( NewAge Group ) <marcos.vaz@newage-software.com.br>
  0 siblings, 1 reply; 2+ messages in thread

From: Marcos Vaz - \( NewAge Group \) @ 2001-06-06 21:13 UTC (permalink / raw)
  To: pgsql-sql

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




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: sum of string columns, why ?
@ 2001-06-18 16:41  Ross J. Reedstrom <reedstrm@rice.edu>
  parent: Marcos Vaz - ( NewAge Group ) <marcos.vaz@newage-software.com.br>
  0 siblings, 0 replies; 2+ messages in thread

From: Ross J. Reedstrom @ 2001-06-18 16:41 UTC (permalink / raw)
  To: 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



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2001-06-18 16:41 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-06-06 21:13 sum of string columns, why ? Marcos Vaz - ( NewAge Group ) <marcos.vaz@newage-software.com.br>
2001-06-18 16:41 ` Ross J. Reedstrom <reedstrm@rice.edu>

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