using a plpgsql function argument as a table column.

2018-08-28 Thread Shaun Savage
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')

Re: using a plpgsql function argument as a table column.

2018-08-28 Thread Shaun Savage
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.XX  FROM allnames AS t1 JOIN

Re: using a plpgsql function argument as a table column.

2018-08-31 Thread Shaun Savage
I fixed it by changed that data. all the years were in one row so I created a separate row for each year. create table tvalues as select t.key, year, value from values t   join lateral (values     (2021,t.y2021),     (2020,t.y2020),     (2019,t.y2019),     (2018,t.y2018),     (2017,t.y2017),