Monthly Transactions III

4pts
Stripe

Problem Statement

Stripe operates a global payment processing network. A key metric for risk management is the chargeback rate and value relative to approved transactions.

A chargeback happens when a customer disputes a transaction after it has occurred. Even if a transaction was approved in a previous month, a chargeback is recorded based on the date it was filed.

Write an SQL query to find for each month and country:

  1. The number of approved transactions and their total amount.
  2. The number of chargebacks and their total amount.

Rules:

  • Output columns: month, country, approved_count, approved_amount, chargeback_count, chargeback_amount.
  • month should be in YYYY-MM format.
  • Only include months where there is at least one approved transaction or one chargeback.
  • Results must be sorted by month in ascending order, then by country in ascending order.
Tests your understanding of
Union, Aggregation and Date Formatting

Input Tables

Transactions
id(INTEGER)country(VARCHAR)state(VARCHAR)amount(INTEGER)trans_date(DATE)
101USapproved10002019-05-18
102FRdeclined20002019-05-19
103USapproved30002019-06-10
104USapproved40002019-06-13
105FRapproved50002019-06-15
Chargebacks
trans_id(INTEGER)trans_date(DATE)
1022019-05-29
1012019-06-30
1052019-09-18
Merchants
merchant_id(INTEGER)merchant_name(VARCHAR)category(VARCHAR)
1AppleTech
2Stripe ShopRetail
3BakeryFood
4GymHealth
5AirlineTravel
6HotelTravel

Expected Output

month(VARCHAR)country(VARCHAR)approved_count(INTEGER)approved_amount(INTEGER)chargeback_count(INTEGER)chargeback_amount(INTEGER)
2019-05US1100000
2019-06FR1500000
2019-06US2700011000
2019-09FR0015000

Tags

MediumUnionAggregationDate Formatting
20-25 min
58%

Hints