X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 6791B9DCB60 for ; Sun, 26 Mar 2006 03:03:23 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 17153-07 for ; Sun, 26 Mar 2006 03:03:21 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id EF3469DCD13 for ; Sun, 26 Mar 2006 03:02:19 -0400 (AST) Received: from fep06.ttnet.net.tr (mail.ttnet.net.tr [212.175.13.129]) by svr4.postgresql.org (Postfix) with ESMTP id 26CC45AF9E4 for ; Sun, 26 Mar 2006 07:02:15 +0000 (GMT) Received: from alamut ([85.102.43.21]) by fep06.ttnet.net.tr with ESMTP id <20060326065735.QAPC2935.fep06.ttnet.net.tr@alamut>; Sun, 26 Mar 2006 09:57:35 +0300 Date: Sun, 26 Mar 2006 08:56:25 +0300 From: Volkan YAZICI To: george young Cc: pgsql-sql@postgresql.org Subject: Re: Expressing a result set as an array (and vice versa)? Message-ID: <20060326055625.GA185@alamut> References: <20060325221102.31de88d7.gry@ll.mit.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060325221102.31de88d7.gry@ll.mit.edu> User-Agent: Mutt/1.4.2.1i X-NAI-Spam-Rules: 0 Rules triggered X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=2.058 required=5 tests=[AWL=-0.740, DNS_FROM_RFC_ABUSE=0.479, DNS_FROM_RFC_POST=1.44, DNS_FROM_RFC_WHOIS=0.879] X-Spam-Score: 2.058 X-Spam-Level: ** X-Archive-Number: 200603/410 X-Sequence-Number: 24756 On Mar 25 10:11, george young wrote: > On Mar 23 11:44, Don Maier 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.