X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7FB1252802 for ; Wed, 18 May 2005 13:14:15 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 90766-01 for ; Wed, 18 May 2005 16:14:12 +0000 (GMT) Received: from out1.smtp.messagingengine.com (out1.smtp.messagingengine.com [66.111.4.25]) by svr1.postgresql.org (Postfix) with ESMTP id CCFD8528D6 for ; Wed, 18 May 2005 13:13:58 -0300 (ADT) Received: from frontend3.messagingengine.com (frontend3.internal [10.202.2.152]) by frontend1.messagingengine.com (Postfix) with ESMTP id E08C3C8F967 for ; Wed, 18 May 2005 12:13:33 -0400 (EDT) X-Sasl-enc: hk1Dd3LMuyEQN0cU09dKE1qWTwF9cW2BdLvh4KBjSbmJWE/6dZ2yGtArtAQTrA 1116432812 Received: from ibm550.klee (pD95E3148.dip.t-dialin.net [217.94.49.72]) by frontend3.messagingengine.com (Postfix) with ESMTP id 6324684 for ; Wed, 18 May 2005 12:13:31 -0400 (EDT) Date: Wed, 18 May 2005 18:12:37 +0200 Message-ID: <87zmus8qdm.wl%felix.klee@inka.de> From: "Felix E. Klee" To: pgsql-sql@postgresql.org Subject: Turning column into *sorted* array? User-Agent: Wanderlust/2.14.0 (Africa) SEMI/1.14.6 (Maruoka) FLIM/1.14.7 (=?ISO-8859-4?Q?Sanj=F2?=) APEL/10.6 Emacs/21.3 (i486-slackware-linux-gnu) MULE/5.0 (SAKAKI) X-Smtpserver: mail.messagingengine.com:25 X-Newsserver: localhost:1119 MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200505/150 X-Sequence-Number: 21463 Suppose I have a table containing a column with integer values: CREATE TABLE some_table (x int NOT NULL); INSERT INTO some_table values(1); INSERT INTO some_table values(5); INSERT INTO some_table values(3); Also, there is a view to that table (probably superfluous here, but it's in the actual more complicated database design): CREATE VIEW some_view AS SELECT x FROM some_table; Now, the goal is to turn the column "x" of the view "some_view" into a *sorted* array. I tried the following code. It works, but is it reliable? IOW: is it guaranteed that this gives me indeed a sorted array? If not, what's a good alternative? CREATE AGGREGATE array_accum ( sfunc = array_append, basetype = anyelement, stype = anyarray, initcond = '{}' ); COMMENT ON AGGREGATE array_accum(anyelement) IS 'Found in section "33.9. User-Defined Aggregates" of the PostgreSQL 7.4.2 Documentation.'; SELECT array_accum(x) FROM (SELECT * FROM some_view ORDER BY x) AS tmp; BTW, the best alternative (in terms of execution performance) that comes into my mind is to create an aggregate that does the sorting right away while the values "come in" from the rows. But that'd probably take me some time to get right. -- Felix E. Klee