Stripe tracks all payment attempts in a table called charges:
charges (
charge_id TEXT PRIMARY KEY,
merchant_id INTEGER,
amount INTEGER, -- in USD cents
currency TEXT, -- e.g. 'USD', 'EUR', 'GBP'
status TEXT, -- 'succeeded', 'failed', 'processing'
created_at TIMESTAMP
)
payment_methods (
payment_method_id TEXT PRIMARY KEY,
charge_id TEXT,
merchant_id INTEGER,
method_type TEXT, -- 'card', 'ach', 'wire', 'apple_pay'
created_at TIMESTAMP
)
disputes (
dispute_id TEXT PRIMARY KEY,
charge_id TEXT,
merchant_id INTEGER,
reason TEXT, -- 'fraudulent', 'duplicate', 'chargeback', 'general'
created_at TIMESTAMP
)
Write a SQL query that computes the daily merchant payment metrics for the last 30 days:
- total_charges: count of all charge attempts
- successful_charges: count of succeeded charges
- volume_usd: sum of successful charge amounts (in dollars, not cents)
- dispute_count: count of distinct disputes related to that day's charges
- success_rate_pct: (successful / total) * 100, rounded to 2 decimals
- dispute_rate_pct: (disputes / successful) * 100, rounded to 2 decimals (0 if no successful charges)
Output: payment_date, merchant_id, total_charges, successful_charges, volume_usd, dispute_count, success_rate_pct, dispute_rate_pct
Order by payment_date DESC, merchant_id ASC.