View a markdown version of this page

合併範例 - Amazon Redshift

Amazon Redshift 將不再支援在 2026 年 6 月 30 日之後使用 Python UDFs。我們將開始分階段強制執行。如需 Python 生命週期結束和遷移選項的詳細資訊,請參閱 2025 年 6 月 30 日發佈的部落格文章

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

合併範例

下列範例會執行合併以更新 SALES 資料表。第一個範例使用較簡易的方法,就是從目標資料表刪除,然後從臨時資料表插入所有資料列。第二個範例需要更新目標資料表中的特定資料欄,因此包括額外的更新步驟。

合併範例 會使用名為 TICKIT 資料集的 Amazon Redshift 範例資料集。作為先決條件,您可以依照開始使用一般資料庫任務指南中的指示,設定 TICKIT 資料表和資料。有關範例資料集的更多詳細資訊,請參閱範例資料庫

範例合併資料來源

本節中的範例使用包含更新和插入的範例資料來源。若要設定範例資料,請從 SALES 資料表建立名為 SALES_UPDATE 的資料表,並填入代表 12 月新銷售活動的隨機資料。下列範例使用 SALES_UPDATE 資料表做為預備資料表。

-- Create a sample table as a copy of the SALES table. create table tickit.sales_update as select * from tickit.sales; -- Change every fifth row to have updates. update tickit.sales_update set qtysold = qtysold*2, pricepaid = pricepaid*0.8, commission = commission*1.1 where saletime > '2008-11-30' and mod(sellerid, 5) = 0; -- Add some new rows to have inserts. -- This example creates a duplicate of every fourth row. insert into tickit.sales_update select (salesid + 172456) as salesid, listid, sellerid, buyerid, eventid, dateid, qtysold, pricepaid, commission, getdate() as saletime from tickit.sales_update where saletime > '2008-11-30' and mod(sellerid, 4) = 0;

根據相符索引鍵取代現有資料列的合併範例

下列範例使用 SALES_UPDATE 資料表,在 SALES 資料表上執行合併操作,其中包含 12 月銷售活動的新資料。合併會取代 SALES 資料表中具有更新的資料列,修改 qtysoldpricepaid資料欄,同時保持不變commissionsaletime

MERGE into tickit.sales USING tickit.sales_update sales_update on ( sales.salesid = sales_update.salesid and sales.listid = sales_update.listid and sales_update.saletime > '2008-11-30' and (sales.qtysold != sales_update.qtysold or sales.pricepaid != sales_update.pricepaid)) WHEN MATCHED THEN update SET qtysold = sales_update.qtysold, pricepaid = sales_update.pricepaid WHEN NOT MATCHED THEN INSERT (salesid, listid, sellerid, buyerid, eventid, dateid, qtysold , pricepaid, commission, saletime) values (sales_update.salesid, sales_update.listid, sales_update.sellerid, sales_update.buyerid, sales_update.eventid, sales_update.dateid, sales_update.qtysold , sales_update.pricepaid, sales_update.commission, sales_update.saletime); -- Drop the staging table. drop table tickit.sales_update; -- Test to see that commission and salestime were not impacted. SELECT sales.salesid, sales.commission, sales.salestime, sales_update.commission, sales_update.salestime FROM tickit.sales INNER JOIN tickit.sales_update sales_update ON sales.salesid = sales_update.salesid AND sales.listid = sales_update.listid AND sales_update.saletime > '2008-11-30' AND (sales.commission != sales_update.commission OR sales.salestime != sales_update.salestime);

指定資料欄清單而不使用 MERGE 的合併範例

下列範例使用 SALES_UPDATE 資料表作為資料來源,在 SALES 資料表上執行合併操作,其中包含 12 月活動的新資料。範例資料包含更新、插入和未變更的資料列。合併會更新 qtysoldpricepaid資料欄,但會保留 commissionsaletime 不變。

-- Create a staging table and populate it with rows from SALES_UPDATE for Dec create temp table stagesales as select * from sales_update where saletime > '2008-11-30'; -- Start a new transaction begin transaction; -- Update the target table using an inner join with the staging table -- The join includes a redundant predicate to collocate on the distribution key –- A filter on saletime enables a range-restricted scan on SALES update sales set qtysold = stagesales.qtysold, pricepaid = stagesales.pricepaid from stagesales where sales.salesid = stagesales.salesid and sales.listid = stagesales.listid and stagesales.saletime > '2008-11-30' and (sales.qtysold != stagesales.qtysold or sales.pricepaid != stagesales.pricepaid); -- Delete matching rows from the staging table -- using an inner join with the target table delete from stagesales using sales where sales.salesid = stagesales.salesid and sales.listid = stagesales.listid; -- Insert the remaining rows from the staging table into the target table insert into sales select * from stagesales; -- End transaction and commit end transaction; -- Drop the staging table drop table stagesales;