Search Postgresql Archives

Re: different unnest function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



-----Original Message-----
From: pgsql-general-owner@xxxxxxxxxxxxxx [mailto:pgsql-general-owner@xxxxxxxxxxxxxx] On Behalf Of Ondrej Ivanic
Sent: Friday, September 16, 2011 12:54 AM
To: pgsql-general@xxxxxxxxxxxxxx general
Subject:  different unnest function

Hi,

I need function which unnest array in a different way. Input table has ineger[][] column:
col1
----------
{{1,2,3,4}, {5,6,7,8}, {9, 10, 11, 12}}
{{11,12,13,14}, {15,16,17,18}, {19, 110, 111, 112}} ...

and output should be:

select unnest2(col1) from T
unnest2
-----------------
{1,2,3,4}
{5,6,7,8}
{9, 10, 11, 12}
{11,12,13,14}
{15,16,17,18}
{19, 110, 111, 112}

My function is:
create or replace function unnest2(anyarray) returns setof anyarray AS $BODY$ select $1[i:i] from generate_series(array_lower($1,1), array_upper($1,1)) i; $BODY$ language 'sql';

and the result is:
{{1,2,3,4}}
{{5,6,7,8}}
{{9, 10, 11, 12}}
{{11,12,13,14}}
{{15,16,17,18}}
{{19, 110, 111, 112}}

which is almost what I need...(or I'm at the beginning :))  Any ideas?

----------------------------------------------------------------------------

Untested by try something like:

SELECT ($1[i:i])[1] FROM generate_series(...) i;

Basically you want to take the first (and only) array element from your slice.  You might need to use sub-queries to get the syntax to work but the idea should be possible.

David J.




-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux