【问题标题】:#Update and Append in Teradata. /* Update existing records in table 1 from table 2. Append new records in table 1 if table 2 have new record */#在 Teradata 中更新和追加。 /* 从表 2 更新表 1 中的现有记录。如果表 2 有新记录,则在表 1 中追加新记录 */
【发布时间】:2019-12-01 21:17:35
【问题描述】:

我有 table1 作为

我有table2

我需要用 table2 值更新 table1。两个表中相同的 mbr_id 记录应替换为 table2 值。 table2 中的新 mbr_id 应附加到 table1。

预期输出:

我的方法

  1. 从 table2 中分离现有的 mbr_id 和新的 mbr_id
create table set1 as select * from table2 where mbr_id not in (select mbr_id from table1)
create table set2 as select *  from table2 where mbr_id in (select mbr_id from table1)
  1. 将 set1 附加到 table1
insert into table1 select * from set1
  1. 用 set2 更新 table1
UPDATE t1
FROM table1  t1,
set2 t2
SET 
sales_bucket = t2.sales_bucket,
pro = t2.pro,
Region = t2.Region
where t1.mbr_id = t2.mbr_id

问题是:有没有更好的方法一次性完成同样的任务?

【问题讨论】:

  • 是的,有。检查MERGE 语法。
  • 我会试试@dnoeth

标签: sql teradata


【解决方案1】:

MERGE 语句将 UPDATE 和 INSERT 语句组合成一个带有两个条件测试子句的语句: 匹配时,更新。 不匹配时,插入。 您还可以使用 MERGE 语句通过指定:WHEN MATCHED, DELETE 来删除行。

【讨论】:

  • 谢谢@Mohammed。我正在使用合并
猜你喜欢
  • 2021-04-12
  • 2016-06-10
  • 1970-01-01
  • 2021-06-08
  • 2018-04-08
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多