0% completed
Problem
Table: Transactions
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| id | int |
| country | varchar |
| state | enum |
| amount | int |
| trans_date | date |
+----------------+---------+
id is the column of unique values of this table.
The table has information about incoming transactions.
The state column is an ENUM (category) of type ["approved", "declined"].
Table: Chargebacks
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| trans_id | int |
| trans_date | date |
+----------------+---------+
Chargebacks contains basic information regarding incoming chargebacks from some transactions placed in Transactions table.
trans_id is a foreign key (reference column) to the id column of Transactions table.
Each chargeback corresponds to a transaction made previously even if they were not approved.
Problem Definition
Write a solution to the number of approved transactions and their total amount, the number of chargebacks, and their total amount find for each month and country. Return the records in the order of date.
Note: In your solution, given the month and country, ignore rows with all zeros.
Example
Output
Try It Yourself
Solution
To determine the number of approved transactions and their total amount, the number of chargebacks and their total amount for each month and country, we can follow a structured approach that leverages SQL's aggregation and conditional functions. This ensures accurate and efficient computation by combining relevant data from the Transactions
and Chargebacks
tables.
- Combine Chargebacks with Approved Transactions: Merge chargeback records with approved transactions, labeling chargebacks distinctly to differentiate them from regular approved transactions.
- Aggregate Data by Month and Country: Calculate the count and total amount of approved transactions and chargebacks for each combination of month and country.
- Filter Out Rows with All Zeros: Exclude any month-country combinations where both approved transactions and chargebacks are zero, focusing only on meaningful data.
- Order the Results: Sort the final output chronologically by month to present the data in an organized manner.
SQL Query
SELECT month, country, Sum(CASE WHEN state = "approved" THEN 1 ELSE 0 END) AS approved_count, Sum(CASE WHEN state = "approved" THEN amount ELSE 0 END) AS approved_amount, Sum(CASE WHEN state = "back" THEN 1 ELSE 0 END) AS chargeback_count, Sum(CASE WHEN state = "back" THEN amount ELSE 0 END) AS chargeback_amount FROM (SELECT LEFT(Chargebacks.trans_date, 7) AS month, country, "back" AS state, amount FROM Chargebacks JOIN Transactions ON Chargebacks.trans_id = Transactions.id UNION ALL SELECT LEFT(trans_date, 7) AS month, country, state, amount FROM Transactions WHERE state = "approved") s GROUP BY month, country HAVING approved_count > 0 OR chargeback_count > 0 ORDER BY month;
Step-by-Step Approach
Step 1: Combine Chargebacks with Approved Transactions
Merge the Chargebacks
and Transactions
tables to create a unified dataset that distinguishes chargebacks from approved transactions. This involves labeling chargebacks with a unique state identifier and ensuring that only approved transactions are included from the Transactions
table.
SQL Query:
SELECT LEFT(Chargebacks.trans_date, 7) AS month, country, "back" AS state, amount FROM Chargebacks JOIN Transactions ON Chargebacks.trans_id = Transactions.id UNION ALL SELECT LEFT(trans_date, 7) AS month, country, state, amount FROM Transactions WHERE state = "approved";
Explanation:
-
First Part (Chargebacks):
LEFT(Chargebacks.trans_date, 7) AS month
:- Extracts the year and month from the
trans_date
field, formatting it as'YYYY-MM'
.
- Extracts the year and month from the
country
:- Retrieves the country associated with the transaction.
"back" AS state
:- Labels the record as a chargeback by assigning the state
'back'
.
- Labels the record as a chargeback by assigning the state
amount
:- Captures the transaction amount.
JOIN Transactions ON Chargebacks.trans_id = Transactions.id
:- Links each chargeback to its corresponding transaction to access the
country
andamount
.
- Links each chargeback to its corresponding transaction to access the
-
Second Part (Approved Transactions):
LEFT(trans_date, 7) AS month
:- Extracts the year and month from the
trans_date
field, formatting it as'YYYY-MM'
.
- Extracts the year and month from the
country
:- Retrieves the country associated with the transaction.
state
:- Captures the state of the transaction, which is
'approved'
based on theWHERE
clause.
- Captures the state of the transaction, which is
amount
:- Captures the transaction amount.
WHERE state = "approved"
:- Filters the records to include only those transactions that were approved.
-
UNION ALL
:- Combines the results from both parts into a single dataset without removing duplicates.
Output After Step 1:
Based on the provided input data, the combined dataset (s
) will be:
month | country | state | amount |
---|---|---|---|
2019-05 | US | back | 2000 |
2019-06 | US | back | 1000 |
2019-09 | US | back | 5000 |
2019-05 | US | approved | 1000 |
2019-06 | US | approved | 3000 |
2019-06 | US | approved | 5000 |
Explanation of Output:
- Chargebacks:
- Transaction ID 102:
- Chargeback date:
2019-05-29
→ month:2019-05
- Amount:
2000
- Chargeback date:
- Transaction ID 101:
- Chargeback date:
2019-06-30
→ month:2019-06
- Amount:
1000
- Chargeback date:
- Transaction ID 105:
- Chargeback date:
2019-09-18
→ month:2019-09
- Amount:
5000
- Chargeback date:
- Transaction ID 102:
- Approved Transactions:
- Transaction ID 101:
- Date:
2019-05-18
→ month:2019-05
- Amount:
1000
- Date:
- Transaction ID 103:
- Date:
2019-06-10
→ month:2019-06
- Amount:
3000
- Date:
- Transaction ID 105:
- Date:
2019-06-15
→ month:2019-06
- Amount:
5000
- Date:
- Transaction ID 101:
Step 2: Aggregate Data by Month and Country
For each combination of month and country, calculate:
- The number of approved transactions (
approved_count
). - The total amount of approved transactions (
approved_amount
). - The number of chargebacks (
chargeback_count
). - The total amount of chargebacks (
chargeback_amount
).
SQL Query:
SELECT month, country, Sum(CASE WHEN state = "approved" THEN 1 ELSE 0 END) AS approved_count, Sum(CASE WHEN state = "approved" THEN amount ELSE 0 END) AS approved_amount, Sum(CASE WHEN state = "back" THEN 1 ELSE 0 END) AS chargeback_count, Sum(CASE WHEN state = "back" THEN amount ELSE 0 END) AS chargeback_amount FROM ( -- Combined Data from Step 1 SELECT LEFT(Chargebacks.trans_date, 7) AS month, country, "back" AS state, amount FROM Chargebacks JOIN Transactions ON Chargebacks.trans_id = Transactions.id UNION ALL SELECT LEFT(trans_date, 7) AS month, country, state, amount FROM Transactions WHERE state = "approved" ) s GROUP BY month, country HAVING approved_count > 0 OR chargeback_count > 0 ORDER BY month;
Explanation:
SELECT month, country
:- Groups the aggregated data by each month and country combination.
Sum(CASE WHEN state = "approved" THEN 1 ELSE 0 END) AS approved_count
:- Counts the number of approved transactions by summing 1 for each record where the state is
'approved'
.
- Counts the number of approved transactions by summing 1 for each record where the state is
Sum(CASE WHEN state = "approved" THEN amount ELSE 0 END) AS approved_amount
:- Calculates the total amount of approved transactions by summing the
amount
where the state is'approved'
.
- Calculates the total amount of approved transactions by summing the
Sum(CASE WHEN state = "back" THEN 1 ELSE 0 END) AS chargeback_count
:- Counts the number of chargebacks by summing 1 for each record where the state is
'back'
.
- Counts the number of chargebacks by summing 1 for each record where the state is
Sum(CASE WHEN state = "back" THEN amount ELSE 0 END) AS chargeback_amount
:- Calculates the total amount of chargebacks by summing the
amount
where the state is'back'
.
- Calculates the total amount of chargebacks by summing the
FROM (...) s
:- Uses the combined dataset from Step 1 as a subquery aliased as
s
.
- Uses the combined dataset from Step 1 as a subquery aliased as
GROUP BY month, country
:- Aggregates the sums and counts for each unique combination of month and country.
HAVING approved_count > 0 OR chargeback_count > 0
:- Filters out any month-country combinations where both approved transactions and chargebacks are zero, as per the problem's requirement.
ORDER BY month
:- Sorts the final results chronologically by month.
Output After Step 2:
Based on the combined dataset from Step 1, the aggregation would yield:
month | country | approved_count | approved_amount | chargeback_count | chargeback_amount |
---|---|---|---|---|---|
2019-06 | US | 2 | 8000 | 1 | 1000 |
2019-05 | US | 1 | 1000 | 1 | 2000 |
2019-09 | US | 0 | 0 | 1 | 5000 |
Explanation of Output:
- For May 2019 (US):
- Approved Transactions:
- 1 transaction (amount: 1000).
- Total approved amount: 1000.
- Chargebacks:
- 1 chargeback (amount: 2000).
- Total chargeback amount: 2000.
- Approved Transactions:
- For June 2019 (US):
- Approved Transactions:
- 2 transactions (amounts: 3000, 5000).
- Total approved amount: 8000.
- Chargebacks:
- 1 chargeback (amount: 1000).
- Total chargeback amount: 1000.
- Approved Transactions:
- For September 2019 (US):
- Approved Transactions:
- 0 transactions.
- Total approved amount: 0.
- Chargebacks:
- 1 chargeback (amount: 5000).
- Total chargeback amount: 5000.
- Approved Transactions:
Note:
- The
HAVING
clause ensures that only records with at least one approved transaction or one chargeback are included. Thus, even though September 2019 has zero approved transactions, it is included because there is one chargeback.
Step 3: Order the Results by Month
Ensure that the final output is sorted in chronological order based on the month to provide a clear and organized view of the data over time.
SQL Query:
ORDER BY month;
Explanation:
ORDER BY month
:- Sorts the aggregated results in ascending order of the
month
column, ensuring that earlier months appear before later ones.
- Sorts the aggregated results in ascending order of the
Final Output:
Based on the aggregated data from Step 2, the final output after ordering would be:
month | country | approved_count | approved_amount | chargeback_count | chargeback_amount |
---|---|---|---|---|---|
2019-05 | US | 1 | 1000 | 1 | 2000 |
2019-06 | US | 2 | 8000 | 1 | 1000 |
2019-09 | US | 0 | 0 | 1 | 5000 |
Explanation of Output:
- The results are ordered chronologically by month (
2019-05
,2019-06
,2019-09
), providing a sequential view of transaction activities and chargebacks for the US.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible