Re: How to unnest an array with element indexes

From: AlexK <alkuzo(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: How to unnest an array with element indexes
Date: 2014-02-19 19:57:48
Message-ID: 1392839868185-5792774.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

Can you confirm that this behavior is guaranteed and documented. I could not
find it in the docs.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/How-to-unnest-an-array-with-element-indexes-tp5792770p5792774.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Pavel Stehule 2014-02-19 20:08:12 Re: How to unnest an array with element indexes
Previous Message David Johnston 2014-02-19 19:32:23 Re: How to unnest an array with element indexes