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 4B7B39FB1D5 for ; Tue, 2 May 2006 17:58:24 -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 59088-09 for ; Tue, 2 May 2006 17:58:14 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from wr-out-0506.google.com (wr-out-0506.google.com [64.233.184.227]) by postgresql.org (Postfix) with ESMTP id A8A279FB1D3 for ; Tue, 2 May 2006 17:58:14 -0300 (ADT) Received: by wr-out-0506.google.com with SMTP id 67so6258wri for ; Tue, 02 May 2006 13:58:13 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:user-agent:x-accept-language:mime-version:to:subject:content-type:content-transfer-encoding; b=DKs8cd0kPoiKvbjRwKVKViK3SvAiXeDGpnBUFq1HS+/IB8aSXeEuETCYlLpnloZ9/nL6Y9D7WFNWzlRABCe4vzZALjIl2zz7g9sMGlUnq4SJT1olTtJlns0YRT3LnK38Ys2hLRZABNJlerx31xfE3mb7sAFC4gFNWLI8cy15Xy8= Received: by 10.54.157.1 with SMTP id f1mr211032wre; Tue, 02 May 2006 13:56:34 -0700 (PDT) Received: from ?172.17.4.249? ( [201.10.50.178]) by mx.gmail.com with ESMTP id g2sm802443wra.2006.05.02.13.58.10; Tue, 02 May 2006 13:58:11 -0700 (PDT) Message-ID: <4457C86F.8040303@gmail.com> Date: Tue, 02 May 2006 18:00:31 -0300 From: =?ISO-8859-1?Q?Everton_Lu=EDs_Berz?= User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: pt-br, pt MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Sorting aggregate column contents Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/16 X-Sequence-Number: 25047 Is it possible to sort the content of an aggregate text column? Query: select s.name, ag_concat(c.name) from state s inner join city c on (c.idstate = s.idstate) group by s.name order by s.name; Result: name | ag_concat -------+--------------------------- RS | Porto Alegre, Gramado SP | Osasco (2 rows) Expected result: name | ag_concat -------+--------------------------- RS | Gramado, Porto Alegre SP | Osasco (2 rows) I tried "order by s.name, c.name" but it causes a error: ERROR: column "c.name" must appear in the GROUP BY clause or be used in an aggregate My function and aggregate code: CREATE FUNCTION f_concat (text, text) RETURNS text AS $$ DECLARE t text; BEGIN IF character_length($1) > 0 THEN t = $1 || ', ' || $2; ELSE t = $2; END IF; RETURN t; END; $$ LANGUAGE plpgsql; CREATE AGGREGATE ag_concat ( sfunc = f_concat, basetype = text, stype = text, initcond = '' ); -- Everton