Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WGCsi-0000us-GL for pgsql-sql@arkaria.postgresql.org; Wed, 19 Feb 2014 19:32:32 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WGCsh-00045p-Mu for pgsql-sql@arkaria.postgresql.org; Wed, 19 Feb 2014 19:32:31 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WGCsg-00045i-KU for pgsql-sql@postgresql.org; Wed, 19 Feb 2014 19:32:30 +0000 Received: from sam.nabble.com ([216.139.236.26]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WGCsZ-0007Rg-M4 for pgsql-sql@postgresql.org; Wed, 19 Feb 2014 19:32:29 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1WGCsZ-0005qN-42 for pgsql-sql@postgresql.org; Wed, 19 Feb 2014 11:32:23 -0800 Date: Wed, 19 Feb 2014 11:32:23 -0800 (PST) From: David Johnston To: pgsql-sql@postgresql.org Message-ID: <1392838343105-5792771.post@n5.nabble.com> In-Reply-To: <1392837956043-5792770.post@n5.nabble.com> References: <1392837956043-5792770.post@n5.nabble.com> Subject: Re: How to unnest an array with element indexes MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 3.7 (+++) 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 wrote > Given an array such as ARRAY[1.1,1.2], I need to select both values and > indexes, as follows: > > 1;1.1 > 2;1.2 > > The following query does what I want for a simple example: > > with pivoted_array AS( > select unnest(ARRAY[1.1,1.2]) > ) > select ROW_NUMBER() OVER() AS element_index, unnest as element_value > from pivoted_array > > Is ROW_NUMBER() OVER() guaranteed to always return array's index? If not, > how should I predictably/deterministically do it? 9.4 will provide for this capability directly. For earlier releases as long as the next and only thing you do after unnesting the array is apply the window function the order will be consistent - the rows will be seen by the window in array order. You must not perform any other joins until the row numbers have been assigned. It is best to use a pair of CTE/WITH queries to accomplish this and then use the result of the second CTE in the main query. If your need is much more complicated than the simple example provided you may wish to give something more close to your actual need for some to opine on. David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/How-to-unnest-an-array-with-element-indexes-tp5792770p5792771.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql