agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Tom Lane <tgl@sss.pgh.pa.us>
To: AlexK <alkuzo@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: How to unnest an array with element indexes
Date: Wed, 19 Feb 2014 15:16:10 -0500
Message-ID: <13954.1392840970@sss.pgh.pa.us> (raw)
In-Reply-To: <1392839868185-5792774.post@n5.nabble.com>
References: <1392837956043-5792770.post@n5.nabble.com>
<1392838343105-5792771.post@n5.nabble.com>
<1392839868185-5792774.post@n5.nabble.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>
AlexK <alkuzo@gmail.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
--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
view thread (8+ messages) latest in thread
Message-ID: <13954.1392840970@sss.pgh.pa.us>
Permalink: ../13954.1392840970@sss.pgh.pa.us/
Also on: postgresql.org/message-id/13954.1392840970@sss.pgh.pa.us
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, alkuzo@gmail.com
Subject: Re: How to unnest an array with element indexes
In-Reply-To: <13954.1392840970@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox