Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 2AA1C9066D0 for ; Thu, 7 Jun 2012 20:33:41 -0300 (ADT) Received: from smtp103.prem.mail.ac4.yahoo.com ([76.13.13.42]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1ScmCw-0004mf-0J for pgsql-sql@postgresql.org; Thu, 07 Jun 2012 23:33:40 +0000 Received: (qmail 27333 invoked from network); 7 Jun 2012 23:33:24 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=C3tG7DSoXDHVpxf2rdQK7pqDkEml1Hjyw8DScN265ev50Q/aeoe2PtHyo3G0gX0x/nG568o7KEUJah2mDK9g+nyJGofIBpRwD6rlpEJNe5NzaCj3SKf6RQBIVVT0LI4JRn2AWHYrlRsHprIq4hUddfj62SACXrtUmJKh7ehFqSo= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1339112004; bh=833U5d7vkcvIghKN3AsO35sYcSp/WvnyvUesRcNj6LE=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=coxwcBOchvzLzZnD2u9GgxAm8Zdi6NuZIq7x0uCItFVyVcv66AN32oC+Xq1ygr590fuUvDz3u1U5hhpiYP8o7/nIsEXYvDgtLn2AGzHlWmTlhTgl7x388KXkGhjdMALkJp6QN7oxkxqCj5535uL7aaE5k14kG3QDPPnr61VAAq4= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: w1xJJbwVM1mJh4OgAf_NXPKCpUakZ1GCGEP6XsXBX9PoWww JG2A5B_Atj1KC3fdVvxvdnuViQwyevC8iMIN_nGV5ZsKviYVzfu5A9pn1bj0 j9o6h2ow.6q6lsXZaDxWpj8jC5oBYaa.8gHuyzPDS3i8F9_kt9urTIFXNGn1 PPWsVksryZYiCcx5NxhRlwjiDqBbKeHsKsEyI5pNuJrraqTAS4E.c1zOk3xC EJbiOINPV37iU27mIMNF4OkTIwjbo06L8VY1pPr38uVFwJfYR11TetvGOdnG yLGxSINpM3hgRV7tRQxf8NNqg4OMsCfLimF8ydUx9cnx4X4o1HO5R7YfjBMG xUMW447qiVtO25dTxGGfom6B4gL.sYzNsyInhnjUSUkSWYkEp2MV1ywUeZiK jNWGmYX6iOhJUwPk5178b X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp103.prem.mail.ac4.yahoo.com with SMTP; 07 Jun 2012 16:33:24 -0700 PDT From: "David Johnston" To: , References: <4FD1368E.5000304@jfcomputer.com> In-Reply-To: <4FD1368E.5000304@jfcomputer.com> Subject: Re: using ordinal_position Date: Thu, 7 Jun 2012 19:32:52 -0400 Message-ID: <00c601cd4505$dcbab8f0$96302ad0$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQJLF5WZvPgQqqk0iRgbbEm+oG+9RpXz2LqQ Content-Language: en-us X-Pg-Spam-Score: -2.0 (--) X-Archive-Number: 201206/26 X-Sequence-Number: 36680 > -----Original Message----- > From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql- > owner@postgresql.org] On Behalf Of John Fabiani > Sent: Thursday, June 07, 2012 7:18 PM > To: pgsql-sql@postgresql.org > Subject: [SQL] using ordinal_position > > I'm attempting to retrieve data using a select statement without knowing the > column names. I know the ordinal position but not the name of the column > (happens to be a date::text and I have 13 fields). > > Below provides the name of the column in position 3: > > select column_name from (select column_name::text, ordinal_position from > information_schema.columns where > table_name='wk_test') as foo where ordinal_position = 3; > > But how can I use the above as a column name in a normal select statement. > > Unlike other databases I just can't use ordinal position in the select > statement - RIGHT??? > > Johnf > This seems like a seriously messed up requirement but I guess the easiest way would be as follows: SELECT tbl.col3 FROM (SELECT * FROM table) tbl (col1, col2, col3) Basically you select ALL columns (thus not caring about their names and always getting the defined order) from the table and give explicit aliases to columns 1 though N where N is the desired column position you want to return. All subsequent columns will retain their original names. If the parent query you can then simply select the column alias you assigned to the desired column position. If you query the catalog for the true column name you would have to use pl/pgSQL and EXECUTE to run the query against a manually built (stringified) query; SQL proper does not allow for table and column names to be variable. That said you may find it worthwhile to publish the WHY behind your inquiry to see if some other less cumbersome and error-prone solution can be found. While column order is fairly static it is not absolute and if the column order were to change you would have no way of knowing. At least when using actual column names the query would fail with an unknown column name exception instead of possibly silently returning bad data. The extra layer of indirection just seems dirty to me - but aside from the possibility of column order changes I don't see any major downsides. Since you are just dealing with column aliases it should not meaningfully impact query plan generation and thus it should be no slower than a more direct query. David J.