Write a snowflake merge command to update the table DNA_ANALYTICS_DB.DBT_PJACQUEMAIN_CONSUMPTION.SALES_NRT_RPTG_TBL
From the query
SELECT
PACIFIC_SRC_BUSINESS_DT,
PACIFIC_START_TM,
SALESGROUP,
OCCASION,
TIMEZONE_ID,
PREFERRED_REGION_NM,
COMP_FISCAL_PRD_IND,
COMP_FISCAL_PRD_YR2_IND,
REFUND_IND,
TOTAL_NET_AMT,
USD_ACTUAL_TOTAL_NET_AMT,
TRANSACTION_CNT,
ZERO_VALUE_TRANSACTION_CNT,
NON_ZERO_VALUE_TRANSACTION_CNT
FROM
DNA_PROD_DB.CONSUMPTION.SALES_NRT_RPTG_VW
I want the result to be that the tables are equal after the merge. If there was a record in the target table that is no longer in the source query it should be deleted from the target table. This should be done as a merge in a single command
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a Snowflake MERGE command that will synchronize the target table DNA_ANALYTICS_DB.DBT_PJACQUEMAIN_CONSUMPTION.SALES_NRT_RPTG_TBL with the source query from DNA_PROD_DB.CONSUMPTION.SALES_NRT_RPTG_VW:
sql
MERGE INTO DNA_ANALYTICS_DB.DBT_PJACQUEMAIN_CONSUMPTION.SALES_NRT_RPTG_TBL AS target
USING (
SELECT
PACIFIC_SRC_BUSINESS_DT,
PACIFIC_START_TM,
SALESGROUP,
OCCASION,
TIMEZONE_ID,
PREFERRED_REGION_NM,
COMP_FISCAL_PRD_IND,
COMP_FISCAL_PRD_YR2_IND,
REFUND_IND,
TOTAL_NET_AMT,
USD_ACTUAL_TOTAL_NET_AMT,
TRANSACTION_CNT,
ZERO_VALUE_TRANSACTION_CNT,
NON_ZERO_VALUE_TRANSACTION_CNT
FROM
DNA_PROD_DB.CONSUMPTION.SALES_NRT_RPTG_VW
) AS source
ON
target.PACIFIC_SRC_BUSINESS_DT = source.PACIFIC_SRC_BUSINESS_DT AND
target.PACIFIC_START_TM = source.PACIFIC_START_TM AND
target.SALESGROUP = source.SALESGROUP AND
target.OCCASION = source.OCCASION AND
target.TIMEZONE_ID = source.TIMEZONE_ID AND
target.PREFERRED_REGION_NM = source.PREFERRED_REGION_NM AND
target.COMP_FISCAL_PRD_IND = source.COMP_FISCAL_PRD_IND AND
target.COMP_FISCAL_PRD_YR2_IND = source.COMP_FISCAL_PRD_YR2_IND AND
target.REFUND_IND = source.REFUND_IND
WHEN MATCHED THEN
UPDATE SET
target.TOTAL_NET_AMT = source.TOTAL_NET_AMT,
target.USD_ACTUAL_TOTAL_NET_AMT = source.USD_ACTUAL_TOTAL_NET_AMT,
target.TRANSACTION_CNT = source.TRANSACTION_CNT,
target.ZERO_VALUE_TRANSACTION_CNT = source.ZERO_VALUE_TRANSACTION_CNT,
target.NON_ZERO_VALUE_TRANSACTION_CNT = source.NON_ZERO_VALUE_TRANSACTION_CNT
WHEN NOT MATCHED THEN
INSERT (
PACIFIC_SRC_BUSINESS_DT,
PACIFIC_START_TM,
SALESGROUP,
OCCASION,
TIMEZONE_ID,
PREFERRED_REGION_NM,
COMP_FISCAL_PRD_IND,
COMP_FISCAL_PRD_YR2_IND,
REFUND_IND,
TOTAL_NET_AMT,
USD_ACTUAL_TOTAL_NET_AMT,
TRANSACTION_CNT,
ZERO_VALUE_TRANSACTION_CNT,
NON_ZERO_VALUE_TRANSACTION_CNT
)
VALUES (
source.PACIFIC_SRC_BUSINESS_DT,
source.PACIFIC_START_TM,
source.SALESGROUP,
source.OCCASION,
source.TIMEZONE_ID,
source.PREFERRED_REGION_NM,
source.COMP_FISCAL_PRD_IND,
source.COMP_FISCAL_PRD_YR2_IND,
source.REFUND_IND,
source.TOTAL_NET_AMT,
source.USD_ACTUAL_TOTAL_NET_AMT,
source.TRANSACTION_CNT,
source.ZERO_VALUE_TRANSACTION_CNT,
source.NON_ZERO_VALUE_TRANSACTION_CNT
)
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
Explanation:
Merge Logic:
The MERGE command will synchronize the target table with the source query
It uses all columns except the numeric metrics as the join condition (assuming these are the natural key columns)
Operations:
WHEN MATCHED: Updates the numeric metric columns when a record exists in both tables
WHEN NOT MATCHED: Inserts new records from the source that don't exist in the target
WHEN NOT MATCHED BY SOURCE: Deletes records from the target that no longer exist in the source
Result:
After execution, the target table will exactly match the source query results
All records in the target that don't exist in the source will be deleted
All records in the source will be inserted or updated in the target