diff --git a/sql/build_master_tables.sql b/sql/build_master_tables.sql index 85b4ace..bcbbbac 100644 --- a/sql/build_master_tables.sql +++ b/sql/build_master_tables.sql @@ -9,16 +9,16 @@ BEGIN FOR f IN SELECT 'DROP TABLE IF EXISTS fc.'||func||'; CREATE TABLE IF NOT EXISTS fc.'||func||' (' || - string_agg(cname || ' ' || dtype,', ' ORDER BY opos ASC) || - ', PRIMARY KEY ('||string_agg(cname,', ') FILTER (WHERE fkey = func)||'));' AS ddl, + string_agg(format('%I',cname) || ' ' || dtype,', ' ORDER BY opos ASC) || + ', PRIMARY KEY ('||string_agg(format('%I',cname),', ') FILTER (WHERE fkey = func)||'));' AS ddl, ---need to add a clause to exclude where the key is null - 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(cname,', ' ORDER BY opos ASC) || ' FROM rlarp.osm_dev WHERE '||string_agg(cname,'||') FILTER (WHERE fkey = func)||' IS NOT NULL ON CONFLICT DO NOTHING' AS pop + 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(format('%I',cname),', ' ORDER BY opos ASC) || ' FROM rlarp.osm_dev WHERE '||string_agg(format('%I',cname)||' IS NOT NULL ',' AND ') FILTER (WHERE fkey = func)||' ON CONFLICT DO NOTHING' AS pop FROM fc.target_meta - WHERE - func <> 'doc' GROUP BY func + HAVING + string_agg(cname,', ') FILTER (WHERE fkey = func) <> '' loop EXECUTE format('%s',f.ddl); EXECUTE format('%s',f.pop); diff --git a/sql/temp.sql b/sql/temp.sql index 0c6db84..ee6f28a 100644 --- a/sql/temp.sql +++ b/sql/temp.sql @@ -1,12 +1,12 @@ SELECT 'DROP TABLE IF EXISTS fc.'||func||'; CREATE TABLE IF NOT EXISTS fc.'||func||' (' || - string_agg(cname || ' ' || dtype,', ' ORDER BY opos ASC) || - ', PRIMARY KEY ('||string_agg(cname,', ') FILTER (WHERE fkey = func)||'));' AS ddl, + string_agg(format('%I',cname) || ' ' || dtype,', ' ORDER BY opos ASC) || + ', PRIMARY KEY ('||string_agg(format('%I',cname),', ') FILTER (WHERE fkey = func)||'));' AS ddl, ---need to add a clause to exclude where the key is null - 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(cname,', ' ORDER BY opos ASC) || ' FROM rlarp.osm_dev WHERE '||string_agg(cname,'||') FILTER (WHERE fkey = func)||' IS NOT NULL ON CONFLICT DO NOTHING' AS populate + 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(format('%I',cname),', ' ORDER BY opos ASC) || ' FROM rlarp.osm_dev WHERE '||string_agg(format('%I',cname)||' IS NOT NULL ',' AND ') FILTER (WHERE fkey = func)||' ON CONFLICT DO NOTHING' AS pop FROM fc.target_meta - WHERE - func <> 'doc' GROUP BY func + HAVING + string_agg(cname,', ') FILTER (WHERE fkey = func) <> ''