From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | AlexK <alkuzo(at)gmail(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: How to unnest an array with element indexes |
Date: | 2014-02-19 20:16:10 |
Message-ID: | 13954.1392840970@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
AlexK <alkuzo(at)gmail(dot)com> writes:
> David,
> The array stores a time series of values for consecutive days. All I need is
> take an array such as ARRAY[1.1,1.2] and return to the client the following
> series_start_date + (array_index-1), array_value
> Based on what you are saying, the following should do it:
> with pivoted_array AS(
> select unnest(ARRAY[1.1,1.2])
> ),
> indexed_array AS(
> select
> row_number()OVER() AS element_index,
> unnest as element_value
> from pivoted_array)
> SELECT
> (DATE '2014-02-19' + INTERVAL '1d'*(element_index-1)) AS
> series_date,
> element_value AS series_value
> FROM indexed_array
That sure seems like the hard way, as well as uncertainly correct.
I'd use something involving generate_subscripts(), for example if
tab.arraycol is the source of the array data:
select i, arraycol[i] from tab, lateral generate_subscripts(arraycol, 1) as i;
If you're using a version of Postgres that doesn't have LATERAL, it's
a bit more complicated but you can do it with a subquery.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | AlexK | 2014-02-19 20:55:42 | Re: How to unnest an array with element indexes |
Previous Message | Pavel Stehule | 2014-02-19 20:08:12 | Re: How to unnest an array with element indexes |