[英]What is the right syntax for updating table on Redshift? (code example)
我想創建一個計划查詢來每天更新 Redshift 上的現有表。
我的想法是,我想每天添加新行,其中包含使用唯一 key1 和 key2 發生的事件。 此外,我想更新上周在 value1、value2 或 value3 中更新的行,但我只想插入這些值,前提是它們在原始產品表中為空(因此我無法刪除行並再次插入它們key1 和 key2)。 我想保留一個多星期前發生的行不受影響。
我缺少正確的語法可能是因為下面的代碼不起作用。 查詢臨時表工作正常。 你能告訴我我在這里缺少什么嗎?
帶有示例的 Redshift 文檔在這里: https ://docs.amazonaws.cn/en_us/redshift/latest/dg/merge-specify-a-column-list.html
create temp table [temp_table_name] as
[SELECT * FROM temp_table_name];
begin transaction;
update [prod_table_name]
set value1 = NVL(value1, temp_table_name.value1),
value2 = NVL(value2, temp_table_name.value1),
value3 = NVL(value3, temp_table_name.value1)
from [temp_table_name]
where key1 = temp_table_name.key1
and key2 = temp_table_name.key2
and key3 = temp_table_name.key3
and key4 = temp_table_name.key4;
insert into [prod_table_name]
select *
from [temp_table_name]
where value1 <> temp_table_name.value1
and value2 <> temp_table_name.value2;
end transaction;
我遇到了各種不同類型的錯誤,但無法找出問題出在哪里。
我懷疑問題是“key1”和“value1”的歧義。 我認為您需要在 WHERE 子句中按名稱指定目標表。 像這樣:
where prod_table_name.key1 = temp_table_name.key1
和:
where prod_table_name.value1 <> temp_table_name.value1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.