Write SQL to extract specific data sets relevant to an expense management implementation scenario.
Use the provided departments, employees, merchants, and transactions data. Return finalized transactions from 2025, excluding declined and reversed transactions, and treat missing merchant classifications as Unclassified. Compare each department and merchant category with its previous available reporting month.
Output
- One row per department, reporting month, and merchant category with finalized activity.
- Columns:
department_name, reporting_month, merchant_category, monthly_spend, transaction_count, previous_month_spend, spend_change_pct.
- Sort by department name, reporting month, and merchant category ascending.
Output details
- Restrict transactions to statuses
posted and settled.
- Include transactions on or after
2025-01-01 and before 2026-01-01.
- Represent
reporting_month as the first day of each month.
- Round
spend_change_pct to two decimal places.
- Return
NULL for previous_month_spend when no earlier reporting month exists, and when prior spend is zero, return NULL for spend_change_pct.