You are given ERP actuals and a planning tool extract for the same fiscal months. Write a PostgreSQL query that reconciles the two sources by account_code and fiscal_month, returns the actual amount, planned amount, and variance, and flags each row as matched, actual_only, plan_only, or mismatch when the numbers do not tie.
Output details
- Return columns in this order:
account_code, fiscal_month, actual_amount, planned_amount, variance, reconciliation_status.
- Sort ascending by
account_code, then ascending by fiscal_month.
- Use exact equality for matched amounts and compute
variance as actual minus planned without rounding or tolerance.
- Set
variance to NULL unless both amounts are present, preserving NULL for the missing amount.
- Label rows with only actuals as
actual_only and rows with only plans as plan_only.