CREATE SINK insights.client_to_account_groups_sink AS
SELECT
m.client_id,
m.account_group_id,
m.type
FROM insights.client_to_account_groups_mv AS m
LEFT JOIN insights.client_account_group_types_mv AS cagt
ON cagt.client_id = m.client_id AND cagt.type = m.type
WHERE
m.type = 'none' OR NOT cagt.client_id IS NULL
WITH (
connector='postgres',
host='psql-nonprod-uae-derived.postgres.database.azure.com',
port='6432',
user='migration_user',
password='***',
database='alinma-qa-insights-db',
table='client_to_account_groups_dm',
schema='insights',
type='upsert',
force_compaction='true',
primary_key='client_id,account_group_id,type'
)