Re: Query Problem - Mailing list pgsql-sql

From Joe Conway
Subject Re: Query Problem
Date
Msg-id 3FB039BB.9090103@joeconway.com
Whole thread Raw
In response to Query Problem  (Abdul Wahab Dahalan <wahab@mimos.my>)
List 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




pgsql-sql by date:

Previous
From: ow
Date:
Subject: Re: pg 7.4.rc1, Range query performance
Next
From: Bruce Momjian
Date:
Subject: Re: pg 7.4.rc1, Range query performance