{x}
blog image

Monthly Transactions II

Solution Explanation for Monthly Transactions II

This problem requires analyzing two tables, Transactions and Chargebacks, to generate a summary of monthly transactions for each country. The summary includes the number and total amount of approved transactions and chargebacks.

Approach

The solution uses a WITH clause (Common Table Expression) to create a unified view of transactions and chargebacks. This simplifies the subsequent aggregation.

  1. Unified View (CTE T): A CTE named T combines data from Transactions and Chargebacks. It uses a UNION to append chargeback information to the transaction data. The chargeback rows are identified by setting the state to 'chargeback'. Crucially, it joins Transactions and Chargebacks based on id and trans_id to link chargebacks to their corresponding transactions.

  2. Aggregation: The main SELECT statement aggregates data from T. It performs the following:

    • DATE_FORMAT(trans_date, '%Y-%m') AS month: Extracts the year and month from the transaction date.
    • SUM(state = 'approved') AS approved_count: Counts approved transactions (MySQL treats boolean TRUE as 1 and FALSE as 0).
    • SUM(IF(state = 'approved', amount, 0)) AS approved_amount: Sums the amounts of approved transactions.
    • SUM(state = 'chargeback') AS chargeback_count: Counts chargebacks.
    • SUM(IF(state = 'chargeback', amount, 0)) AS chargeback_amount: Sums the amounts of chargebacks.
    • GROUP BY 1, 2: Groups the results by month and country.
    • HAVING approved_amount OR chargeback_amount: Filters out rows where both approved and chargeback amounts are zero, as specified in the problem statement.

MySQL Code

WITH
    T AS (
        SELECT * FROM Transactions
        UNION
        SELECT id, country, 'chargeback', amount, c.trans_date
        FROM
            Transactions AS t
            JOIN Chargebacks AS c ON t.id = c.trans_id
    )
SELECT
    DATE_FORMAT(trans_date, '%Y-%m') AS month,
    country,
    SUM(state = 'approved') AS approved_count,
    SUM(IF(state = 'approved', amount, 0)) AS approved_amount,
    SUM(state = 'chargeback') AS chargeback_count,
    SUM(IF(state = 'chargeback', amount, 0)) AS chargeback_amount
FROM T
GROUP BY 1, 2
HAVING approved_amount OR chargeback_amount;

Time Complexity Analysis

The time complexity is dominated by the UNION operation and the GROUP BY operation. The UNION operation has a complexity of O(m+n), where 'm' is the number of rows in Transactions and 'n' is the number of rows in Chargebacks. The GROUP BY operation has a complexity that depends on the sorting algorithm used, but generally is O(k log k) where k is the number of rows after the UNION. Overall, the time complexity is approximately O(max(m,n) * log(m+n)) , which can be considered close to O(N logN) where N is the total number of rows after joining both tables.

Space Complexity Analysis

The space complexity is determined by the size of the CTE T, which is proportional to the total number of rows in the Transactions and Chargebacks tables. Therefore, the space complexity is O(m+n).

This solution efficiently combines data from two tables to generate the required summary, with a reasonable time and space complexity given the constraints of the problem.