Re: Expressing a result set as an array (and vice versa)? - Mailing list pgsql-sql

From Volkan YAZICI
Subject Re: Expressing a result set as an array (and vice versa)?
Date
Msg-id 20060326055625.GA185@alamut
Whole thread Raw
In response to Re: Expressing a result set as an array (and vice versa)?  (george young <gry@ll.mit.edu>)
List pgsql-sql
On Mar 25 10:11, george young wrote:
> On Mar 23 11:44, Don Maier <dMaier@genome.stanford.edu> wrote:
> > 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?
>
> Not so easy without a custom function.

But not that hard:

test=> SELECT id, val FROM t_arr;id |      val      
----+--------------- 1 | {1,2,3} 2 | {4,5,6} 3 | {7,8,9} 4 | {10,11,12,13}
(4 rows)

--
-- First Way
--
test=> SELECT id, val[s.i]
test-> FROM t_arr
test-> LEFT OUTER JOIN
test->   (SELECT g.s
test(>      FROM generate_series(1, (SELECT max(array_upper(val, 1)) FROM t_arr)) AS g(s)
test(>   ) AS s(i)
test->   ON (s.i <= array_upper(val, 1));id | val 
----+----- 1 |   1 1 |   2 1 |   3 2 |   4 2 |   5 2 |   6 3 |   7 3 |   8 3 |   9 4 |  10 4 |  11 4 |  12 4 |  13
(13 rows)

--
-- Second Way (by using contrib/intagg)
--
SELECT id, int_array_enum(val) FROM t_arr;


Regards.


pgsql-sql by date:

Previous
From: george young
Date:
Subject: Re: Expressing a result set as an array (and vice versa)?
Next
From: Markus Schaber
Date:
Subject: Problem using set-returning functions