Write a SQL query to calculate the daily failure rate by payment method and merchant segment for the last 90 days on a payment platform. Use the supplied transaction and reference tables. Treat a transaction with status FAILED as a failure, and retain records whose payment method or merchant segment is unavailable.
Output
- One row per transaction date, payment method, and merchant segment within the last 90 days.
- Columns:
transaction_date, payment_method, merchant_segment, total_transactions, failed_transactions, and failure_rate.
- Return
failure_rate as a percentage rounded to two decimal places, ordered by date descending, payment method, and merchant segment.
Output details
- Treat
DATE '2026-09-15' as today's date.
- Include transactions from
2026-06-17 inclusively through before 2026-09-16.
- Replace unavailable payment methods and merchant segments with
UNKNOWN.
- Order payment methods and merchant segments ascending after ordering transaction dates descending.