Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fv0hj-000488-CB for pgsql-general@arkaria.postgresql.org; Wed, 29 Aug 2018 13:40:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fv0hh-00082L-47 for pgsql-general@arkaria.postgresql.org; Wed, 29 Aug 2018 13:40:13 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fv0hg-00082C-0v for pgsql-general@lists.postgresql.org; Wed, 29 Aug 2018 13:40:12 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fv0hd-0005UZ-5h for pgsql-general@lists.postgresql.org; Wed, 29 Aug 2018 13:40:10 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 167302200E; Wed, 29 Aug 2018 09:40:08 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 29 Aug 2018 09:40:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=KOZWQSjrUdQKn2bB6/UfBX5BRsnEB ymJo0uDBfrgW+8=; b=ufXhIYFwzqVIpFdqo8xQnMFSBt7WN1XQzoFU/gseVgMw1 ZfFOr/HcdaQp7+30tUzeLu591vfBByFhtnNhhwDBeMcGyuo0srDBmiEQEdSo60aT KiowVrX0CUj9uxmzPmRGvW7svSVySkiZcJ3x2tYTN1iAklDXz95vo0ek7Gxislhl o1UcpnMmhXarvqD2C+bjoSJ//kS3mL09Za0evIaOJfuACQye6Qm/uQhpFhOAepiy c4kp9usUajNplnHh/QHlJBrb4fIb/40nD5p8jNAolJseO2n+yc5bb4PXssI83M5K Mno3N58919qHvY8ofVnR4NoFkNY4NHW38ZdjrAbuQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=KOZWQS jrUdQKn2bB6/UfBX5BRsnEBymJo0uDBfrgW+8=; b=Q41Zq+TI5x57+UaJMowLYh zHb/9FG7S18EidzEtcLQHzNlzZWuLYVDPIxUB0f/++QHZQGXl4uTYmMegEru1bRI LPn6H1nqnFhJrni3WOY7hpYGMzQXZAsm5Al67HtADWEOX9A9P7M2wkOTN7qKcqAs evJG9KKx+FXxJW+zINbK2394xnjpd1AnkgeNKJk0wNDjP8yKOBPkdizKJ7O1Z6bJ RUDAtjTSlG3waUapf2H5C+M+/+yapZRopbivu2jPeLwqQm0YOcsP2gZHllIAVfCv Fkx8zCmtyThw1J+FN1ZO2l5cBdqAPRdWc2QSHkaYgWKjC1tJ86FFZD2C6VvNFOAw == X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id D1D0B10294; Wed, 29 Aug 2018 09:40:06 -0400 (EDT) Subject: Re: using a plpgsql function argument as a table column. To: ss , Tim Cross Cc: "pgsql-generallists.postgresql.org" References: <837526ef-2ea4-5569-9d01-1e56b82037bf@tuxclub.org> From: Adrian Klaver Message-ID: Date: Wed, 29 Aug 2018 06:40:06 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 08/28/2018 10:40 PM, ss wrote: > > CREATE OR REPLACE FUNCTION test(year VARCHAR) > RETURNS TABLE (agencycode INT, bureaucode INT, acctname VARCHAR, beacat > VARCHAR, onoffbudget VARCHAR, val INT) > AS $$ > BEGIN > RETURN QUERY SELECT t1.agencycode, t1.bureaucode, t1.acctcode, > t2.beacat, t2.onoffbudget, t2.XXXXXX  FROM allnames AS t1 > JOIN total AS t2 on t1.agencycode = t2.agencycode and t1.bureaucode = > t2.bureaucode and t1.acctcode = t2.acctcode > WHERE t2.XXXXXXX != 0  ORDER BY t1.agencycode, t1.bureaucode, t1.acctcode; > END; $$ > LANGUAGE 'plpgsql'; > > I want to replace XXXXXXX with the argument year. > I hard coded the XXXXXX with 'y2016' and I get back a table So something along lines of: DO $$ DECLARE col_name varchar; out_str varchar; BEGIN col_name = 'y'||'2018'; out_str = format('SELECT %I FROM some_table where %I = 0', col_name, col_name ); RAISE NOTICE '%', out_str; END; $$ LANGUAGE plpgsql; NOTICE: SELECT y2018 FROM some_table where y2018 = 0 > > Would something like 't2.'||(year)|| work? > > On 08/28/2018 10:37 PM, Tim Cross wrote: >> Off the top of my head, I think you could do this using dynamic >> (execute) SQL in a function. However, it is going to be messy, >> possibly slow and likely fragile. You would need to query the >> catalogue to get the column names in the table and then build the SQL >> dynamically 'on the fly'. >> >> Without having more detail, my spider sense tells me you have the >> wrong table/relationship design. While you may be able to get it to >> work, it is likely you will run into constant problems and additional >> complexity that could be avoided with a different design. You really >> want a design where your queries are driven by the data in your tables >> and not by the names of columns. I would seriously consider >> re-examining your schema design, look at how your design fits in with >> the normal forms and adapt as necessary. >> >> Tim >> >> On Wed, 29 Aug 2018 at 15:10, ss > > wrote: >> >> I have a table with many years as columns. y1976, y2077, .. , >> y2019,y2020 I want to dynamically return a column from a function. >> >> >> select * from FUNCTION('y2016') ..... >> >> select t1.cola t1.colb, t1.colc, t2.y2016 from ..... Where >> t2.y2016 != 0; >> >> or if I select year y2012 I want FUNCTION('y2012') >> >> select t1.cola t1.colb, t1.colc, t2.y2012 from ..... Where >> t2.y2012 != 0; >> >> >> to generalize >> >> select * from FUNCTION( year_column ) >> >> select t1.cola t1.colb, t1.colc, t2.year_column from ..... Where >> t2.year_column != 0; >> >> is it possible? if so how? >> >> >> >> >> -- >> regards, >> >> Tim >> >> -- >> Tim Cross >> > -- Adrian Klaver adrian.klaver@aklaver.com