병합 예 - Amazon Redshift

병합 예

다음 예는 병합을 수행하여 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 테이블을 사용해 12월 영업 활동의 새 데이터로 SALES 테이블에서 병합 작업을 수행합니다. 이 예제에서는 업데이트가 있는 SALES 테이블의 행을 바꿉니다. 이 예제에서는 판매 수량 및 가격 지불 열을 업데이트하지만 수수료 및 급여 시간은 변경하지 않습니다.

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;