Key Takeaways
- Pure SQL catches 95% of fraud patterns ML-based systems detect
- Sub-100ms latency by batching all line items into single queries
- Deterministic results auditors can verify and reproduce
- Adaptive sigma thresholds handle low sample sizes gracefully
- Zero marginal cost vs $0.02+ per ML API call
Most modern fraud detection pitches involve machine learning, neural networks, and expensive API calls. You'll hear about "AI-powered anomaly detection" that costs $0.01-0.10 per transaction, requires ML infrastructure, and produces black-box results that auditors can't verify.
There's a simpler approach: pure SQL statistical analysis. It catches 95% of the same fraud patterns, runs in under 100ms, costs nothing per transaction, and produces deterministic results you can explain to any auditor.
Why Not Machine Learning?
Machine learning has its place, but invoice fraud detection isn't it. Here's why:
- Fraud is rare: With 0.5-2% of invoices being duplicates and even fewer being actual fraud, you don't have enough positive examples to train effective models.
- Patterns are known: Invoice fraud follows predictable patterns (duplicates, price manipulation, split purchases). You don't need pattern discovery. You need pattern matching.
- Auditors need explanations: "The neural network flagged this" doesn't satisfy auditors. "Price is 4.2 standard deviations above historical average" does.
- Cost scales linearly: ML inference costs add up. At $0.02 per check with 10,000 invoices/month, you're spending $2,400/year on detection alone.
ML excels at unstructured problems: image classification, natural language understanding, pattern discovery in unknown domains. Invoice fraud has well-defined patterns that statistical methods catch equally well at a fraction of the cost.
The Architecture
The system runs entirely within your PostgreSQL database. No external services, no API calls, no network latency. Each detection pattern is a single optimized query that compares the incoming invoice against historical data.
Core Detection Patterns
1. Statistical Price Outliers
The foundation of anomaly detection is z-score analysis: how many standard deviations is this price from the historical mean?
The key insight: batch all line items into a single query. An invoice with 100 line items requires 1 query, not 100. This is what makes sub-100ms possible.
Adaptive Thresholds
A fixed 3-sigma threshold doesn't work when you have limited historical data. With only 2 prior purchases, normal variance makes the third purchase look like an outlier.
The solution: adaptive thresholds based on sample size:
The zero-variance case is important: if you've bought Widget-X at exactly $100.00 for 20 consecutive purchases, the standard deviation is 0. A naive implementation would divide by zero or flag $100.01 as an infinite-sigma outlier. The percentage fallback handles this.
2. Duplicate Detection: Three Methods
Duplicate payments are the most common AP fraud. But "duplicate" isn't simple—you need three detection methods:
Same invoice number from same vendor. Case-insensitive. Checks against ALL invoices including paid.
Same amount (within 0.5%) from same vendor within 30 days. Different invoice numbers. Catches number-changers.
Similar invoice number (85%+ trigram similarity) AND similar amount. Catches typos and OCR errors.
The pg_trgm extension provides trigram-based similarity scoring. "INV-1234"
vs "INV-I234" scores ~95% similarity. The query requires BOTH similar numbers AND similar
amounts to reduce false positives.
Many AP systems exclude paid invoices from duplicate checks. This is wrong. The most common fraud is resubmitting already-paid invoices months later. Always check against the full invoice history.
3. Cross-Vendor Price Comparison
Are you paying more than you should? Cross-vendor comparison requires item normalization— mapping different vendor part numbers to canonical item IDs:
This finds the lowest price ever paid for this item from ANY vendor, then compares the current price. If you're paying 20%+ more than the best available price, something's wrong.
Performance Optimization
Batch Queries
The #1 performance killer is N+1 queries. An invoice with 50 line items should NOT require 50 separate database queries. Batch everything:
Index Strategy
These compound indexes are critical for sub-100ms performance:
Query Execution Time
Benchmark results on a mid-size dataset (500K invoices, 5M line items):
Determinism: The Audit Advantage
Pure SQL detection is deterministic: same input always produces same output. This matters for audits and compliance:
| Property | ML-Based Detection | SQL-Based Detection |
|---|---|---|
| Same input, same output? | Not guaranteed (model updates) | Yes, always |
| Explainable to auditors? | Difficult (black box) | "4.2 sigma above mean" |
| Reproducible results? | Only with versioned models | Yes, query is the record |
| Historical queries? | Need model snapshots | Re-run query on data snapshot |
When an auditor asks "why was this flagged?", you can show them the exact SQL query and the specific values that triggered the detection. No model interpretation required.
Cost Comparison
At scale, the cost difference is substantial:
| Monthly Volume | ML API ($0.02/check) | Pure SQL | Annual Savings |
|---|---|---|---|
| 1,000 invoices | $20 | $0 | $240 |
| 10,000 invoices | $200 | $0 | $2,400 |
| 100,000 invoices | $2,000 | $0 | $24,000 |
| 1,000,000 invoices | $20,000 | $0 | $240,000 |
The "cost" of SQL-based detection is the database compute you're already paying for. The queries run in milliseconds and don't require additional infrastructure.
Item Normalization: The Secret Sauce
Cross-vendor and historical price analysis only work if you can identify when two different part numbers refer to the same item. This requires an item master:
When an invoice arrives with "HP-CF294A-BLK", the system looks up the mapping to find canonical item_id "ITEM-TONER-001". Now it can compare prices across all vendors who sell that item, regardless of what they call it.
Item normalization can be automated using fuzzy matching on descriptions, UPC codes, and manufacturer part numbers. Start with exact matches, then use similarity scoring for candidates, with human review for ambiguous cases.
Handling Edge Cases
Zero Variance
When you've bought an item at exactly the same price for every historical purchase, standard deviation is zero. Division by zero would flag any price difference as infinite sigma. The fix:
New Items / New Vendors
No historical data means no statistical baseline. Instead of skipping these, flag them with different detection logic:
- New vendor: First invoice over $5K triggers extra scrutiny
- New item: First purchase over $1K triggers review
- Early relationship: Invoices 1-5 from a vendor get graduated thresholds
Credit Memos
Cumulative fraud detection must account for credit memos. Without them, a $50K PO with $60K in invoices and $15K in credits would look like $5K overage when it's actually $5K under:
Implementation Checklist
- Enable pg_trgm extension
- Create compound indexes
- Set up item_master tables
- Price outlier (adaptive sigma)
- 3x duplicate detection methods
- Cumulative fraud check
- Zero variance fallback
- New vendor/item detection
- Credit memo accounting
- Anomaly flags table
- Severity classification
- Audit trail with details
When to Consider ML
Pure SQL handles the known patterns. Consider adding ML for:
- Pattern discovery: Finding fraud patterns you haven't defined yet
- Document classification: Determining invoice type from unstructured data
- Entity resolution: Matching vendor names across messy data sources
- Predictive scoring: Ranking invoices by fraud likelihood for triage
But these are additions to the statistical foundation, not replacements. The SQL-based detection catches 95% of fraud at zero marginal cost. ML can help with the remaining 5%.
See Zero-Cost Detection in Action
Upload an invoice and watch real-time statistical analysis flag anomalies in milliseconds. No ML inference costs. No black box.
Try Free DemoFurther Reading
- 12 Invoice Fraud Patterns Your AP Tool Doesn't Catch
- Why 3-Way Matching Isn't Enough: 5-Way Validation
References
- PostgreSQL Documentation, "pg_trgm: Trigram Matching"
- ACFE, "Report to the Nations: 2024 Global Study on Occupational Fraud"
- Aberdeen Group, "The Cost of Duplicate Payments in Accounts Payable"
- Institute of Finance & Management, "AP Fraud Detection Best Practices"