Вы находитесь на странице: 1из 4

Store Inventory Merge Example

-- create a staging table to bring and hold the incremental data

create or replace TABLE STG_STORE_INVENTORY (


DATE DATETIME,
STOREID NUMBER(38,0),
STORENAME VARCHAR(150),
PRODUCTID NUMBER(38,0),
PRODUCTNAME VARCHAR(150),
COLOR VARCHAR(50),
SIZE VARCHAR(150),
MANUFACTURER VARCHAR(100),
ONHANDQUANTITY NUMBER(38,0),
ONORDERQUANTITY NUMBER(38,0),
SAFETYSTOCKQUANTITY NUMBER(38,0),
UNITCOST FLOAT,
DAYSINSTOCK NUMBER(38,0),
MININSTOCK NUMBER(38,0),
MAXINSTOCK NUMBER(38,0),
LAST_UPDATE_DTTM TIMESTAMP_NTZ(9) default TO_TIMESTAMP_NTZ(current_timestamp),
primary key (DATE, STOREID, PRODUCTID)
) comment ='Bring and Store the Incremental Store Inventory Data';

--- copy the incremental data and we have incremental file at AWS S3 -
STORE112JUNERESTATEMENT.txt tab separated columns

copy into STG_store_inventory


from @PUBLIC.UDEMY_DATA_STAGE/retailsalesdata/trxinc/
FILE_FORMAT = (format_name=PUBLIC.TSVTEXT error_on_column_count_mismatch = false)
on_error = 'continue'; --- load from AWS S3 and this snowflake instance is in AWS cloud

-- check the results window . you will see as below

-- view the data


select * from STG_store_inventory;
-- the primary key to merge the data from the staging table (STG_store_inventory) to the target
table (store_inventory)

-- Merge statement

Merge into store_inventory target using (


select
DATE,
STOREID,
STORENAME ,
PRODUCTID ,
PRODUCTNAME,
COLOR ,
SIZE ,
MANUFACTURER ,
ONHANDQUANTITY ,
ONORDERQUANTITY ,
SAFETYSTOCKQUANTITY ,
UNITCOST,
DAYSINSTOCK ,
MININSTOCK ,
MAXINSTOCK
from
STG_store_inventory
) STG
on target.DATE = STG.DATE and target.STOREID = STG.STOREID and target.PRODUCTID =
STG.PRODUCTID
when matched then update set -- updating all the existing records where there was a
match on above keys Date, StoreID and ProductID
DATE=STG.DATE,
STOREID=STG.STOREID,
STORENAME=STG.STORENAME,
PRODUCTID=STG.PRODUCTID ,
PRODUCTNAME=STG.PRODUCTNAME,
COLOR=STG.COLOR ,
SIZE=STG.SIZE ,
MANUFACTURER=STG.MANUFACTURER ,
ONHANDQUANTITY= STG.ONHANDQUANTITY,
ONORDERQUANTITY =STG.ONORDERQUANTITY,
SAFETYSTOCKQUANTITY =STG.SAFETYSTOCKQUANTITY ,
UNITCOST=STG.UNITCOST,
DAYSINSTOCK =STG.DAYSINSTOCK,
MININSTOCK = STG.MININSTOCK,
MAXINSTOCK= STG.MAXINSTOCK,
LAST_UPDATE_DTTM =TO_TIMESTAMP_NTZ(current_timestamp)
when not matched then --- Inserting the new records when there was no match on the keys
Date, StoreID and ProductID
insert (
DATE,
STOREID,
STORENAME ,
PRODUCTID ,
PRODUCTNAME,
COLOR ,
SIZE ,
MANUFACTURER ,
ONHANDQUANTITY ,
ONORDERQUANTITY ,
SAFETYSTOCKQUANTITY ,
UNITCOST,
DAYSINSTOCK ,
MININSTOCK ,
MAXINSTOCK,
LAST_UPDATE_DTTM
) values (
STG.DATE,
STG.STOREID,
STG.STORENAME ,
STG.PRODUCTID ,
STG.PRODUCTNAME,
STG.COLOR ,
STG.SIZE ,
STG.MANUFACTURER ,
STG.ONHANDQUANTITY ,
STG.ONORDERQUANTITY ,
STG.SAFETYSTOCKQUANTITY ,
STG.UNITCOST,
STG.DAYSINSTOCK ,
STG.MININSTOCK ,
STG.MAXINSTOCK,
TO_TIMESTAMP_NTZ(current_timestamp)
);

-- In our example we did not have any new records and hence you are seeing zero records
inserted and all the 342 existing records got updated as below: