I have three prepared statements which create tables from different tables (multiple select statements + join + subqueries) to given date and then join two results of prepared statements namely:
CREATE TABLE _table1 AS
EXECUTE statement1('2000-01-01 00:00:00');
CREATE TABLE _table2 AS
EXECUTE statement2('2000-01-01 00:00:00');
CREATE TABLE _table3 AS
EXECUTE statement3('2000-01-01 00:00:00');
CREATE TABLE _table_all AS
SELECT *
FROM _table1
LEFT JOIN
_table3
ON _table1.var1 = _table3.var1;
Statement3 is build on statement2 = select from _table2 (separate to make it more readable).
What I want to do is:
- execute the same code for few dates -> pass date as parameter
- save final table: _table_all as _table_2000-01-01, _table_2000-02-01 itd.
I do not know how to make such a loop / function which creates table and pass table_name and date as parameter.
Expected result:
FOR my_date IN ('2000-01-01 00:00:00', '2001-01-01 00:00:00', '2002-01-01 00:00:00'):
CREATE TABLE _table1 AS
EXECUTE statement1(@my_date);
CREATE TABLE _table2 AS
EXECUTE statement2(@my_date);
CREATE TABLE _table3 AS
EXECUTE statement3(@my_date);
CREATE TABLE _table_@my_date AS
SELECT *
FROM _table1
LEFT JOIN
_table3
ON _table1.var1 = _table3.var1;
DROP TABLE _table1;
DROP TABLE _table2;
DROP TABLE _table3;
Any help?