Customer Segment Consistency Audit
A multi-stage SQL audit workflow that detects customers assigned to conflicting loyalty segments and automatically categorizes each conflict by the business rule that explains it — so analysts only review genuinely ambiguous cases.
PostgreSQL
Data Quality
CRM Analytics
CTEs & Temp Tables
Index Optimization
Audit Pipeline
1
Data Pool
Filter conflicted
accounts
accounts
→
2
Index
8 indexes for
performance
performance
→
3
Lifecycle
Reactivated &
churn-recovery
churn-recovery
→
4
Hard Rules
Policy-defined
segment rules
segment rules
→
5
Nuances
Soft channel
exceptions
exceptions
→
6
Account
Owner & code
consistency
consistency
→
7
Output
Categorized
results
results
Problem Being Solved
- Each customer should map to exactly one loyalty segment (Platinum, Gold, Standard, Freemium)
- Segment conflicts cause misfired campaigns, wrong discount tiers, and inaccurate churn reports
- With tens of thousands of accounts, manual review is infeasible — an automated triage pipeline was needed
Key SQL Techniques
- Chained CTEs — each stage excludes accounts already categorized upstream
- UNNEST + ARRAY to declare rule tables inline without separate schema objects
- IS DISTINCT FROM for null-safe comparisons on optional flag columns
- Temp table indexes to prevent full-table scans across 7 join stages
Inputs
-
customers_active— master customer table -
accounts_master— supplemental ID lookups -
pending_migration— next migration queue
Impact
- Auto-resolves 83% of conflicts — analysts only review the remaining 17%
- Replaces multi-day manual review with a single script run
- Fully reusable — swap in a new account list each cycle
Sample output — illustrates the two result tables. All identifiers are synthetic placeholders.
No real customer data is shown.
Results 2 — Summary by Conflict Reason
38
Lifecycle Rules
124
Tier Hard Rules
57
Tier Nuances
219
Independent Account
81
Needs Review
519
Total Audited
Results 1 — Customer Detail (sample rows)
| Segs | Segments | Conflict Reason | Notes | |
|---|---|---|---|---|
| alice.m@example.com | 2 | PLATINUM, GOLD | Lifecycle Rules | REACTIVATED — transitional segment expected |
| enterprise.co@corp.com | 2 | PLATINUM, SILVER_PLUS | Tier Hard Rules | ENTERPRISE permits multiple segments by policy |
| market.biz@trade.com | 2 | STANDARD, PREMIUM | Tier Hard Rules | MARKETPLACE permits multiple segments by policy |
| smb.owner@startup.io | 2 | GOLD, GROWTH_TIER | Tier Nuances | Soft exception — SMB_ACCOUNTS channel nuance |
| family.acct@home.net | 2 | GOLD, STANDARD | Independent Account | Distinct account codes per segment |
| mystery.user@web.com | 2 | GOLD, FREEMIUM_PLUS | Needs Review | No matching rule — manual review required |
Only Needs Review rows (81 of 519, ~16%) need analyst attention. The other 84% are auto-categorized.