Re: Query Problem

From: Joe Conway <mail(at)joeconway(dot)com>
To: Abdul Wahab Dahalan <wahab(at)mimos(dot)my>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Query Problem
Date: 2003-11-11 01:22:03
Message-ID: 3FB039BB.9090103@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bruce Momjian 2003-11-11 01:54:38 Re: pg 7.4.rc1, Range query performance
Previous Message ow 2003-11-11 01:10:39 Re: pg 7.4.rc1, Range query performance