问题描述
我想在 postgresql 中实现 SCD2。现在,我正在尝试使用“创建规则”,因为我想根据临时表(例如截断和加载的 STG)更新主表(例如 MAIN)。
因此,每当在临时表 (STG) 中插入时,都应自动执行以下操作。
-
如果有新记录(存在于 STG 但不存在于 MAIN),则插入 MAIN/Primary 表 (MAIN),设置标志=1。
-
及以下 2 条 sql 一世。如果主表 (MAIN) 的非键记录已经存在于阶段表 (STG) 并设置活动标志 =1,则更新主表 (MAIN) 的非键记录。 ii.将旧记录标记为非活动可能是在 MAIN 表中将标志设置为 0。
我正在尝试下面的东西,但它不起作用。请建议更好的方法/正确的 sql。它可以与规则/触发器一起使用。
--sql to UPDATE record in MAIN table as active record with flag=1.
create rule r_upd as on insert to STG
where (exists (select 1 from STG where STG.id=NEW.id))
do
update MAIN set flag=1 where id=NEW.id;
--
There is one more rule (or can be done in one sql) required which can set old record to inactive (FLAG=0)
--
--
--sql to INSERT recrord in MAIN table as active record with flag=1
create rule r_ins as on insert to STG
do
insert into MAIN
select id,1 from STG where id not in (select id from MAIN);
谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)