You are given customer profile data from ACME House CRM and ACME House Checkout, and some fields conflict across sources. Write a PostgreSQL query that returns one resolved row per customer for customers who appear in both systems. For each customer, choose the most trustworthy email and city using this logic: prefer the value from the most recently updated record; if the latest value is NULL or an empty string, fall back to the other source. Also return a discrepancy_flag of 'conflict' when the non-blank values differ across the two sources, otherwise 'match_or_missing'.
Output details
- Return columns in this order:
customer_id, resolved_email, resolved_city, discrepancy_flag.
- Order rows by
customer_id ascending.
- Trim surrounding whitespace from
email and city values before treating empty strings as missing or comparing values.
- When multiple checkout records share a customer, use the greatest
updated_at, breaking ties with greatest checkout_id.