pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: Abdul Wahab Dahalan <wahab@mimos.my>
Cc: pgsql-sql@postgresql.org
Subject: Re: Query Problem
Date: Mon, 10 Nov 2003 17:22:03 -0800
Message-ID: <3FB039BB.9090103@joeconway.com> (raw)
In-Reply-To: <3FB03613.7090207@mimos.my>
References: <3FB03613.7090207@mimos.my>

Abdul Wahab Dahalan wrote:
> If I've a table like below.
> 
>> kk     kj    pngk    vote  
>> 01     02       c       10  
>> 01     02       b       5 
> How do I make a query so that I can get a result
> like this?
>>  
>> kk      kj    pngk    vote
>> 01      02    c,b     15  
>>  

create or replace function accum_text(text, text) returns text as 
'select case when $1 = '''' then $2 else $1 || '','' || $2 end' language 
sql;
CREATE AGGREGATE concat(BASETYPE = text, SFUNC = accum_text, STYPE = 
text, INITCOND = '');
create table t(kk text, kj text, pngk text, vote int);
insert into t values('01','02','c',10);
insert into t values('01','02','b',5);

regression=# select kk, kj, concat(pngk), sum(vote) from t group by kk, kj;
  kk | kj | concat | sum
----+----+--------+-----
  01 | 02 | c,b    |  15
(1 row)

HTH,

Joe





view thread (17+ messages)  latest in thread

Message-ID: <3FB039BB.9090103@joeconway.com>
Permalink:  ../3FB039BB.9090103@joeconway.com/
Also on:    postgresql.org/message-id/3FB039BB.9090103@joeconway.com

 · 

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: mail@joeconway.com, wahab@mimos.my
  Subject: Re: Query Problem
  In-Reply-To: <3FB039BB.9090103@joeconway.com>

* 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