How do you optimize a query that is running slowly on a massive financial transaction dataset?
Using the supplied PostgreSQL schema, write an efficient query that summarizes posted transactions during the first quarter of 2025. Include only active accounts with matching customers and merchants.
Output
- One row per customer, merchant category, and month
- Columns:
customer_name, category, month_start, transaction_count, total_amount, prior_month_total
- Sort by customer, category, and month ascending
Output details
- Use an inclusive lower bound of
2025-01-01 and an exclusive upper bound of 2025-04-01 for transaction timestamps.
- Treat
prior_month_total as the preceding available month within each customer-category series, returning NULL when none exists.
- Include negative and zero-valued posted transactions without filtering or deduplicating them.