How do you optimize a slow-performing SQL query when dealing with millions of transaction records?
Using the provided PostgreSQL tables, write an efficient query for posted transactions during the first quarter of 2025. The result should aggregate transaction amounts by month, account type, and merchant category, while also ranking each group within its month.
Output
- One row per month, account type, and merchant category.
- Return
month_start, account_type, merchant_category, transaction_count, total_amount, category_rank, and month_running_total.
- Include unmatched accounts or merchants as
Unknown, sort by month, rank, account type, and merchant category.