Monthly Transactions II

4pts
Stripe

Problem Statement

Stripe needs a monthly consolidated report. You are given two tables: transactions (all initial payment attempts) and chargebacks (disputed payments).

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:

  • A Chargeback event belongs to the month it happened (from the chargebacks table), not the month of the original transaction.
  • If a month/country pair has no approved transactions and no chargebacks, do not include it.
  • Return columns: month, country, approved_count, approved_amount, chargeback_count, chargeback_amount.
  • Results must be sorted by month in ascending order.
Tests your understanding of
Basic SQL, UNION ALL, Aggregation and Date Manipulation

Input Tables

transactions
id(INTEGER)country(VARCHAR)state(VARCHAR)amount(INTEGER)trans_date(DATE)
101USapproved10002019-05-18
102USdeclined20002019-05-19
103USapproved30002019-06-10
104USapproved40002019-06-13
105INapproved50002019-06-15
106INdeclined1002019-06-20
107UKapproved5002019-07-01
chargebacks
trans_id(INTEGER)trans_date(DATE)
1022019-05-29
1012019-06-30
1052019-09-18
1072019-07-15

Expected Output

month(VARCHAR)country(VARCHAR)approved_count(INTEGER)approved_amount(INTEGER)chargeback_count(INTEGER)chargeback_amount(INTEGER)
2019-05US1100012000
2019-06IN1500000
2019-06US2700011000
2019-07UK15001500
2019-09IN0015000

Tags

MediumBasic SQLUNION ALLAggregationDate Manipulation
20-25 min
42%

Hints