
Write a PostgreSQL query to reconcile invoice totals between Literati billing and ERP data for January 2024. Return one row per billing invoice, use the latest ERP snapshot per invoice_number, classify each invoice as match, amount_mismatch, or missing_in_erp, and show the absolute difference when both amounts exist.
| Column | Type | Description |
|---|---|---|
| invoice_id | ||
| invoice_number | ||
| member_id | ||
| invoice_date | ||
| billed_amount |
| Column | Type | Description |
|---|---|---|
| snapshot_id | ||
| invoice_number | ||
| snapshot_date | ||
| erp_amount |
Include `invoice_number`, `billed_amount`, `erp_amount`, `reconciliation_status`, and `amount_difference`.Use only the most recent ERP snapshot for each invoice.Keep billing invoices even if ERP has no match.