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