X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 3125FD1B551 for ; Tue, 11 Nov 2003 01:27:26 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 31488-08 for ; Mon, 10 Nov 2003 21:26:57 -0400 (AST) Received: from joeconway.com (66-146-172-86.skyriver.net [66.146.172.86]) by svr1.postgresql.org (Postfix) with ESMTP id 4835AD1B522 for ; Mon, 10 Nov 2003 21:26:54 -0400 (AST) Received: from [206.19.64.3] (account jconway HELO joeconway.com) by joeconway.com (CommuniGate Pro SMTP 4.1.5) with ESMTP-TLS id 475699; Mon, 10 Nov 2003 17:25:11 -0800 Message-ID: <3FB039BB.9090103@joeconway.com> Date: Mon, 10 Nov 2003 17:22:03 -0800 From: Joe Conway User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) Gecko/20030312 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Abdul Wahab Dahalan Cc: pgsql-sql@postgresql.org Subject: Re: Query Problem References: <3FB03613.7090207@mimos.my> In-Reply-To: <3FB03613.7090207@mimos.my> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200311/107 X-Sequence-Number: 15922 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