我想与大家分享我为Amazon Redshift所做的一些努力,它是SCD(缓慢变化的维度)Type 2.的动态合并语句
此语句假定的内容事先存在:
请注意,我故意没有添加 end_date 列,因为我发现在特定项目的情况下该列不可用。
此语句的作用:
我知道,如果您在正常的SQL环境中进行构建,这并不是一项艰巨的任务,但是您可能知道,Redshift的SQL是“高度修改的”。这意味着大多数普通SQL的功能都像变量和触发器一样被剪切掉了,这使这项任务很难搞清楚(至少对我来说是如此)。
希望这对任何人都有帮助。有一个好吧。
答案 0 :(得分:0)
CREATE OR REPLACE PROCEDURE repo.inserter(table_name IN varchar(256))
AS $$
DECLARE
temp_table_name varchar := table_name || '_temp';
one_liner varchar := temp_table_name || '_one_liner';
query varchar;
query_builder varchar;
rec record;
BEGIN
EXECUTE 'DROP TABLE IF EXISTS ' || temp_table_name;
EXECUTE 'DROP TABLE IF EXISTS ' || one_liner;
EXECUTE 'CREATE TEMP TABLE ' || temp_table_name || ' (column_name varchar(256),ordinal_position int,is_nullable varchar(3));';
EXECUTE 'INSERT INTO ' || temp_table_name || '(column_name,ordinal_position,is_nullable) SELECT column_name,ordinal_position,is_nullable FROM svv_columns WHERE table_schema = ''repo'' AND table_name = ''' || table_name || ''';';
EXECUTE 'CREATE TEMP TABLE ' || one_liner || ' (columns_string varchar(1024));';
EXECUTE 'INSERT INTO ' || one_liner || ' SELECT LISTAGG(column_name,'','') WITHIN GROUP (order by ordinal_position) FROM ' || temp_table_name || ' where column_name not in ( ''scd_key'',''inserted_date'',''is_active'');';
query := 'SELECT column_name FROM ' || temp_table_name || ' WHERE is_nullable = ''NO'' and column_name not in(''scd_key'',''inserted_date'') order by ordinal_position;';
query_builder := 'UPDATE repo.' || table_name || ' SET is_active = 0 FROM dbimports.' || table_name || ' b WHERE ';
FOR rec IN EXECUTE query LOOP
query_builder := query_builder || 'repo.' || table_name || '.' || rec.column_name || ' = b.' || rec.column_name || ' AND ';
END LOOP;
query_builder := RTRIM(query_builder,'AND ') || ';';
EXECUTE query_builder;
query := 'SELECT columns_string FROM ' || one_liner || ';';
FOR rec IN EXECUTE query LOOP
query_builder := 'INSERT INTO repo.' || table_name || '(' || rec.columns_string || ',is_active,inserted_date) (SELECT ' || rec.columns_string || ',1 as is_active,GETDATE() as inserted_date FROM dbimports.' || table_name || ');';
END LOOP;
EXECUTE query_builder;
END;
$$ LANGUAGE plpgsql;