Dataform Merge Statement

I keep facing this issue where dataform highlights an error pertaining the use of the "MERGE" within the workspace

 

 

 

 

 

config {
    type: "incremental",
    database: '**********',
    schema: 'test_incremntal',
    name: 'incremental'
}

-- Step 1: Aggregate data from the source to get the latest date for each content_id
WITH latest_data AS (
    SELECT 
        content_id,
        MAX(date) AS latest_date  -- Get the latest date for each content_id
    FROM `**********.test_incremntal.source_incremental`
    GROUP BY content_id
),
-- Join the aggregated data with the source to get the full row for the latest date
latest_full_data AS (
    SELECT 
        src.content_id,
        src.date AS update_date,
        src.content_name
    FROM `**********.test_incremntal.source_incremental` src
    JOIN latest_data ld
    ON src.content_id = ld.content_id AND src.date = ld.latest_date
)
-- Step 2: Perform the merge operation
MERGE INTO `**********.test_incremntal.incremental` AS tgt
USING latest_full_data AS src
ON tgt.content_id = src.content_id
WHEN MATCHED THEN
    UPDATE SET
        tgt.update_date = src.update_date,
        tgt.content_name = src.content_name
WHEN NOT MATCHED THEN
    INSERT (content_id, update_date, content_name)
    VALUES (src.content_id, src.update_date, src.content_name)

 

 

 

 

 

. The whole idea is to update info within my target table whenever the script is ran...it checks for the latest date.

Example.....
Source Table

content_iddatecontent_name
1232024-09-18batman returns
4562024-09-18spider-man
7892024-09-18game of thrones
1232023-12-13batman
1232022-06-16bat-man
7522024-09-14ghost-busters

Target Table

content_iddatecontent_name
1232024-09-18batman returns
4562024-09-18spider-man
7892024-09-18game of thrones
7522024-09-14ghost-busters

and if the source table gets new info

Source Table

content_iddatecontent_name
1232024-09-18batman returns
4562024-09-18spider-man
7892024-09-18game of thrones
1232023-12-13batman
1232022-06-16bat-man
7522024-09-14ghost-busters
1232024-09-19batman: The scary cut
4562024-09-19spider-man: No way here
7892024-09-19game of thrones: Bald Eagle
7522024-09-19ghost-busters: alien in the house

Expected Target Table:

content_iddatecontent_name
1232024-09-19batman: The scary cut
4562024-09-19spider-man: No way here
7892024-09-19game of thrones: Bald Eagle
7522024-09-19ghost-busters: alien in the house

keeps the same content_id or adds new one if it doesn't exist and then for the ones that do exist it just updates them

1 0 81