merge into {contest_clip_table_name} as target
using (select $1:id as id, $1:created_at as created_at, $1:updated_at as updated_at, $1:duration as duration, $1:is_base_clip as is_base_clip, $1:clip_id as clip_id, $1:contest_id as contest_id, $1:user_id as user_id, date(to_timestamp(created_at)) as p_date, hour(to_timestamp(created_at)) as p_hour from {stage_path} (file_format => 'BASIC_PARQUET_LOADER', pattern => ('.*.parquet'))) as source
on (target.id = source.id and target.p_date = source.p_date and target.p_hour = source.p_hour)
when matched then update set target.created_at = source.created_at, target.updated_at = source.updated_at, target.duration = source.duration, target.is_base_clip = source.is_base_clip, target.clip_id = source.clip_id, target.contest_id = source.contest_id, target.user_id = source.user_id
when not matched then insert (id, created_at, updated_at, duration, is_base_clip, clip_id, contest_id, user_id, p_date, p_hour) values (source.id, source.created_at, source.updated_at, source.duration, source.is_base_clip, source.clip_id, source.contest_id, source.user_id, source.p_date, source.p_hour);

