合併範例 - Amazon Redshift

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

合併範例

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

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

範例合併資料來源

本節中的範例需要包括更新和插入的樣本資料來源。針對範例,我們將建立名為 SALES_UPDATE 的樣本資料表,其使用來自 SALES 資料表的資料。我們將以代表 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 資料表中取代資料列。針對此範例,我們將會更新 qtysold 和 pricepaid 資料欄,但將 commission 和 saletime 維持不變。

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 的合併範例

下列範例會執行合併操作,以 12 月銷售活動的新資料更新 SALES。我們需要包括更新和插入的樣本資料,以及沒有變更的資料列。針對此範例,我們想要更新 QTYSOLD 和 PRICEPAID 資料欄,例將 COMMISSION 和 SALETIME 維持不變。下列指令碼使用 SALES_UPDATE 資料表在 SALES 資料表上執行合併操作。

-- 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;