different unnest function

From: Ondrej Ivanič <ondrej(dot)ivanic(at)gmail(dot)com>
To: "pgsql-general(at)postgresql(dot)org general" <pgsql-general(at)postgresql(dot)org>
Subject: different unnest function
Date: 2011-09-16 04:54:01
Message-ID: CAM6mieJqHeMOpfhtC4m9U431XT758MGwPEEyjw6eT17OQXbPJQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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?

Thanks,
--
Ondrej Ivanic
(ondrej(dot)ivanic(at)gmail(dot)com)

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mamatha_Kagathi_Chan 2011-09-16 05:23:19 Steps to use pl/pgtcl
Previous Message Toby Corkindale 2011-09-16 03:35:45 Re: Upgrading from 9.0->9.1 Ubuntu Best Practices