Re: Expressing a result set as an array (and vice versa)?

From: PFC <lists(at)peufeu(dot)com>
To: "Don Maier" <dMaier(at)genome(dot)stanford(dot)edu>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Expressing a result set as an array (and vice versa)?
Date: 2006-03-24 23:17:08
Message-ID: op.s6xxmukycigqcu@apollo13
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

CREATE OR REPLACE FUNCTION foreach( liste INTEGER[] ) RETURNS SETOF
INTEGER AS $$
DECLARE
i INTEGER;
BEGIN
FOR i IN 1..icount(liste) LOOP
RETURN NEXT liste[i];
END LOOP;
END;
$$ LANGUAGE plpgsql;

CREATE AGGREGATE array_accum (
sfunc = array_append,
basetype = anyelement,
stype = anyarray,
initcond = '{}'
);

SELECT array_accum( DISTINCT list_id ) FROM bookmarks;
array_accum
---------------
{1,2,3,4,5,7}

SELECT * FROM foreach( '{1,2,3,4,5,7}' );
foreach
---------
1
2
3
4
5
7

On Thu, 23 Mar 2006 20:44:32 +0100, Don Maier <dMaier(at)genome(dot)stanford(dot)edu>
wrote:

> Good day,
>
> Is it possible to construct an array from an appropriate select
> expression that generates a result set of unknown cardinality?
> To focus on the simple case: Is it possible to construct a one-
> dimensional array from a select of a single column in a table with an
> unknown number of rows?
>
> Conversely, is it possible to construct a (single column) result set
> from a select expression on a one-dimensional array with an unknown
> number of elements?
>
> Thanks for any hints!
>
> Regards,
> Don Maier
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2006-03-24 23:48:17 Re: Index on nullable column
Previous Message Stephan Szabo 2006-03-24 22:29:10 Re: SQL Query Newbie Help