pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ross J. Reedstrom <reedstrm@rice.edu>
To: pgsql-sql@postgresql.org
Subject: Re: sum of string columns, why ?
Date: Mon, 18 Jun 2001 11:41:37 -0500
Message-ID: <20010618114137.J14955@rice.edu> (raw)
In-Reply-To: <APELJPHCCHOAOLHBEBGCCEACCAAA.marcos.vaz@newage-software.com.br>
References: <APELJPHCCHOAOLHBEBGCCEACCAAA.marcos.vaz@newage-software.com.br>

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



view thread (2+ messages)

Message-ID: <20010618114137.J14955@rice.edu>
Permalink:  ../20010618114137.J14955@rice.edu/
Also on:    postgresql.org/message-id/20010618114137.J14955@rice.edu

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: reedstrm@rice.edu
  Subject: Re: sum of string columns, why ?
  In-Reply-To: <20010618114137.J14955@rice.edu>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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