I'm trying to create a function that will return a table of data. The problem I'm running into is that I want some of the columns to be dynamically created based on what the year is in the Date
column of the joining table.
For example, if a record's date
field has a year of '2016', then I want '2016' to be concatenated with 'subdepartment_name_' to create the column name in the SELECT
query.
Below is what I've written so far.
CREATE OR REPLACE FUNCTION sample_function ()
RETURNS TABLE(subdepartment_name text,
subdepartment_number text,
department_number text,
department_name text,
my_column1 text,
my_column2 text) AS
$$
DECLARE
year TEXT;
BEGIN
year := date_part('year'::text, table1.date);
RETURN QUERY
EXECUTE 'SELECT table2.subdepartment_number_'||year||
',table2.subdepartment_name_'||year||
',table2.department_number_'||year||
',table2.department_name_'||year||
',table1.*
FROM
table1
LEFT JOIN
table2
ON
table1.team = table2.team_number;';
END;
$$
LANGUAGE plpgsql VOLATILE;
When running this function as SELECT * FROM sample_function()
I get this error:
ERROR: missing FROM-clause entry for table "table1"
LINE 1: SELECT date_part('year'::text, table1....
^
QUERY: SELECT date_part('year'::text, table1.date)
CONTEXT: PL/pgSQL function sample_function() line 5 at assignment
********** Error **********
ERROR: missing FROM-clause entry for table "table1"
SQL state: 42P01
Context: PL/pgSQL function sample_function() line 5 at assignment
Is there a simple way to do this that I'm missing?
Thank you!