Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WGDZ3-0002ZD-01 for pgsql-sql@arkaria.postgresql.org; Wed, 19 Feb 2014 20:16:17 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WGDZ2-0003wl-De for pgsql-sql@arkaria.postgresql.org; Wed, 19 Feb 2014 20:16:16 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WGDZ1-0003vM-3F for pgsql-sql@postgresql.org; Wed, 19 Feb 2014 20:16:15 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WGDYy-0008HD-QH for pgsql-sql@postgresql.org; Wed, 19 Feb 2014 20:16:14 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id s1JKGAT5013955; Wed, 19 Feb 2014 15:16:11 -0500 From: Tom Lane To: AlexK cc: pgsql-sql@postgresql.org Subject: Re: How to unnest an array with element indexes 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> Comments: In-reply-to AlexK message dated "Wed, 19 Feb 2014 11:57:48 -0800" Date: Wed, 19 Feb 2014 15:16:10 -0500 Message-ID: <13954.1392840970@sss.pgh.pa.us> X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org AlexK 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