X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 764F89FB1DD for ; Wed, 3 May 2006 01:18:11 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 61501-03 for ; Wed, 3 May 2006 01:17:52 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tamu-relay.tamu.edu (smtp-relay.tamu.edu [165.91.22.120]) by postgresql.org (Postfix) with ESMTP id 276AF9FB1DE for ; Wed, 3 May 2006 01:17:51 -0300 (ADT) Received: from coe.tamu.edu (edsun.coe.tamu.edu [165.91.232.6]) by tamu-relay.tamu.edu (8.13.4/8.13.3) with ESMTP id k434HnAT034349; Tue, 2 May 2006 23:17:49 -0500 (CDT) (envelope-from bkim@coe.tamu.edu) Received: from coe.tamu.edu (coe.tamu.edu [165.91.232.6]) by coe.tamu.edu (8.11.7p2+Sun/8.11.7) with ESMTP id k434HlQ20581; Tue, 2 May 2006 23:17:47 -0500 (CDT) Date: Tue, 2 May 2006 23:17:47 -0500 (CDT) From: "Ben K." To: =?ISO-8859-1?Q?Everton_Lu=EDs_Berz?= cc: Volkan YAZICI , pgsql-sql@postgresql.org Subject: Re: Sorting aggregate column contents In-Reply-To: <4457F83C.7020505@gmail.com> Message-ID: References: <4457C86F.8040303@gmail.com> <20060502211340.GA188@alamut> <4457F83C.7020505@gmail.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Received-SPF: pass (smtp-relay.tamu.edu: 165.91.232.6 is authenticated by a trusted mechanism) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/23 X-Sequence-Number: 25054 > It works fine. But I wouldn't like using subselect's, then if somebody else > knows about an operator or something like that to put on the aggregator, > please tell me. I think the nature of the f_concat makes it difficult to sort, since it simply adds the next value, so if the source table gives value in the order of 'a','c','d','b' there's no way to handle them within f_concat unless you modify and rearrange the previous result string from within f_concat. So the source table (city) should be sorted. I don't know if this is a standard way, but this one seems to do that. ====================================================== select s.name, ag_concat(c.name) from state s inner join (select * from city order by name desc) as c on c.idstate=s.idstate group by s.name order by 1; OR select s.name, ag_concat(c.name) from state s, (select * from city order by name desc) as c where c.idstate = s.idstate group by s.name order by 1; ====================================================== I'm just reordering the source table on the fly. Curiously, if you don't have 'desc' you'll get a reverse ordered list. (z,...,a) I think your needs may also be met without any aggregator as well (there may be marginal cases which I haven't thought of, but I assume they can be handled if needed) ====================================================== select s.name, array_to_string(array(select name from city where idstate = s.idstate order by name),',') from state s; ====================================================== name | array_to_string ------+--------------------- RP | Gramado,Port Alegre SP | Osasco * I see normalization issue here but guess it's not important. Regards, Ben K. Developer http://benix.tamu.edu