Stripe settles merchant funds via periodic payouts. Revenue recognition must reconcile
successfully charged transactions to the payout amounts (accounting for refunds).
Tables:
payouts (payout_id, merchant_id, payout_date DATE, status VARCHAR(20), amount INTEGER)
-- status: 'paid', 'pending', 'failed'
-- amount: in USD cents
charges (charge_id, merchant_id, payout_id, amount INTEGER, currency TEXT, status TEXT, created_at TIMESTAMP)
-- status: 'succeeded', 'failed'
-- amount: in USD cents
refunds (refund_id, charge_id, merchant_id, amount INTEGER, created_at TIMESTAMP)
-- amount: in USD cents
-- A refund relates to one charge; one charge may have multiple refunds
Write a SQL query that:
For each payout, compute:
- charge_count: number of distinct charges in the payout
- recognized_revenue: sum of (charge amount - associated refunds) for all charges in payout, in USD dollars
- payout_amount: the amount in the payout record, in USD dollars
- reconciliation_status: 'matched' if recognized_revenue and payout_amount differ by <= $1.00, 'variance' otherwise
Expected Output:
| Column | Type |
|---|
| payout_id | TEXT |
| merchant_id | INTEGER |
| payout_date | TEXT (YYYY-MM-DD) |
| charge_count | INTEGER |
| recognized_revenue | REAL (USD dollars, 2 decimals) |
| payout_amount | REAL (USD dollars, 2 decimals) |
| reconciliation_status | TEXT ('matched' or 'variance') |
Order by payout_date DESC.