AllianceBernstein's client data is consolidated from multiple customer systems, so the same investor may appear under different capitalization, spacing, or phone formatting. Write a PostgreSQL query to identify active investment accounts belonging to duplicate customer profiles.
Requirements
- Join customer profiles to investment accounts.
- Normalize email addresses and phone numbers before comparing profiles.
- Use window functions to return every active account in a duplicate group, including the duplicate count and a deterministic rank.
- Exclude records with missing matching fields, inactive accounts, and profiles that do not belong to a duplicate group.
Output details
- Return
normalized_email, normalized_phone, account_id, account_number, legal_name, duplicate_account_count, and duplicate_rank in that order.
- Normalize email by trimming, removing all whitespace, and lowercasing; normalize phone by removing every non-digit character.
- Treat an account as active only when its status equals
ACTIVE.
- Exclude only profiles whose email or phone is
NULL; retain non-NULL values that normalize to empty strings.
- Assign
duplicate_rank by ascending opened_on, breaking ties with ascending account_id.
- Order results by ascending
normalized_email, ascending normalized_phone, then ascending duplicate_rank.