-- Merge geo information (country and city) into APP_EVENT table
-- This query enriches the event data with geographic information based on client IP
MERGE INTO {app_event_table_name} AS target
USING (
    SELECT "ip" AS ip, "country" AS country, "city" AS city 
    FROM IDENTIFIER('{geo_table_name}')
) AS source
ON target.CLIENT_IP = source.ip
   AND target.P_DATE = '{p_date}'
   AND target.P_HOUR = {p_hour}
WHEN MATCHED THEN 
    UPDATE SET
        target.COUNTRY = source.country,
        target.CITY = source.city;
