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

From: Volkan YAZICI <yazicivo(at)ttnet(dot)net(dot)tr>
To: george young <gry(at)ll(dot)mit(dot)edu>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Expressing a result set as an array (and vice versa)?
Date: 2006-03-26 05:56:25
Message-ID: 20060326055625.GA185@alamut
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mar 25 10:11, george young wrote:
> On Mar 23 11:44, Don Maier <dMaier(at)genome(dot)stanford(dot)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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bryce Nesbitt 2006-03-26 21:47:34 Bitfields always atomic? Other way to store attributes?
Previous Message george young 2006-03-26 03:11:02 Re: Expressing a result set as an array (and vice versa)?