pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Volkan YAZICI <yazicivo@ttnet.net.tr>
To: george young <gry@ll.mit.edu>
Cc: pgsql-sql@postgresql.org
Subject: Re: Expressing a result set as an array (and vice versa)?
Date: Sun, 26 Mar 2006 08:56:25 +0300
Message-ID: <20060326055625.GA185@alamut> (raw)
In-Reply-To: <20060325221102.31de88d7.gry@ll.mit.edu>
References: <F3E0C44C-6118-41C6-AC71-2B19258F4F07@genome.stanford.edu>
	<20060325221102.31de88d7.gry@ll.mit.edu>

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.



view thread (6+ messages)  latest in thread

Message-ID: <20060326055625.GA185@alamut>
Permalink:  ../20060326055625.GA185@alamut/
Also on:    postgresql.org/message-id/20060326055625.GA185@alamut

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: yazicivo@ttnet.net.tr, gry@ll.mit.edu
  Subject: Re: Expressing a result set as an array (and vice versa)?
  In-Reply-To: <20060326055625.GA185@alamut>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox