Report monthly approved and chargeback counts and amounts per merchant

Quick Overview

Write a PostgreSQL query that reports, for each month and merchant, the count and total amount of approved transactions and of chargebacks. It tests conditional aggregation, grouping dates into months, and keeping month and merchant pairs that have only declined rows.

Report monthly approved and chargeback counts and amounts per merchant

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `transactions` table holds one row per payment attempt, each with a status of `'approved'`, `'declined'` or `'chargeback'`. For each month and `merchant_id`, report the count and total amount of approved transactions and of chargebacks. A month and merchant pair with only `'declined'` rows still appears, with all values `0`. ```hint Watch the empty groups Check what your aggregates return for a pair that has no approved or no chargeback rows. The output needs `0` there. ``` ### Input Table(s) `transactions` | Column | Type | Meaning | | --- | --- | --- | | `id` | INTEGER | Primary key, one row per transaction | | `merchant_id` | INTEGER | Merchant the transaction belongs to, NOT NULL | | `amount` | INTEGER | Transaction amount, a positive integer, NOT NULL | | `status` | VARCHAR(20) | One of `'approved'`, `'declined'`, `'chargeback'`, NOT NULL | | `order_date` | DATE | Date of the transaction, NOT NULL | Primary key: `id`. ### Output Contract | Column | Type | Meaning | | --- | --- | --- | | `month` | TEXT | Calendar month of `order_date`, formatted `YYYY-MM` | | `merchant_id` | INTEGER | The merchant | | `approved_amount` | INTEGER | Sum of `amount` over approved rows, `0` if none | | `approved_count` | INTEGER | Number of approved rows, `0` if none | | `chargeback_amount` | INTEGER | Sum of `amount` over chargeback rows, `0` if none | | `chargeback_count` | INTEGER | Number of chargeback rows, `0` if none | Return one row per (`month`, `merchant_id`) pair that has at least one transaction of any status. Order by `month` ascending, then `merchant_id` ascending. The pair is unique, so the order is fully determined. All values are whole numbers, so no rounding applies. ### Example `transactions` | id | merchant_id | amount | status | order_date | | --- | --- | --- | --- | --- | | 1 | 10 | 100 | approved | 2024-01-03 | | 2 | 10 | 250 | approved | 2024-01-15 | | 3 | 10 | 100 | chargeback | 2024-01-20 | | 4 | 20 | 80 | declined | 2024-01-09 | | 5 | 20 | 40 | declined | 2024-01-30 | | 6 | 10 | 60 | declined | 2024-02-02 | | 7 | 20 | 300 | approved | 2024-02-11 | | 8 | 20 | 50 | chargeback | 2024-02-27 | | 9 | 20 | 70 | chargeback | 2024-02-28 | Expected result: | month | merchant_id | approved_amount | approved_count | chargeback_amount | chargeback_count | | --- | --- | --- | --- | --- | --- | | 2024-01 | 10 | 350 | 2 | 100 | 1 | | 2024-01 | 20 | 0 | 0 | 0 | 0 | | 2024-02 | 10 | 0 | 0 | 0 | 0 | | 2024-02 | 20 | 300 | 1 | 120 | 2 | Merchant 20 in January and merchant 10 in February have only declined rows, so they appear with zeros. In February, merchant 20's two chargebacks add up to 50 + 70 = 120. ### Constraints and Clarifications - The table has at most 10,000 rows, and `id` is the primary key. - `status` is always exactly one of the three lowercase values. No column can be NULL. - `amount` is a positive integer. - Declined rows add nothing to any output column, but they make their month and merchant pair appear. - A pair with no rows at all does not appear. Missing months are not filled in. - Each row counts in the month of its own `order_date`. A chargeback is not matched back to an earlier approved row.

Overview: Write a PostgreSQL query that reports, for each month and merchant, the count and total amount of approved transactions and of chargebacks. It tests conditional aggregation, grouping dates into months, and keeping month and merchant pairs that have only declined rows.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

The transactions table holds one row per payment attempt, each with a status of 'approved', 'declined' or 'chargeback'. For each month and merchant_id, report the count and total amount of approved transactions and of chargebacks. A month and merchant pair with only 'declined' rows still appears, with all values 0.

Input Table(s)

transactions

ColumnTypeMeaning
idINTEGERPrimary key, one row per transaction
merchant_idINTEGERMerchant the transaction belongs to, NOT NULL
amountINTEGERTransaction amount, a positive integer, NOT NULL
statusVARCHAR(20)One of 'approved', 'declined', 'chargeback', NOT NULL
order_dateDATEDate of the transaction, NOT NULL

Primary key: id.

Output Contract

ColumnTypeMeaning
monthTEXTCalendar month of order_date, formatted YYYY-MM
merchant_idINTEGERThe merchant
approved_amountINTEGERSum of amount over approved rows, 0 if none
approved_countINTEGERNumber of approved rows, 0 if none
chargeback_amountINTEGERSum of amount over chargeback rows, 0 if none
chargeback_countINTEGERNumber of chargeback rows, 0 if none

Return one row per (month, merchant_id) pair that has at least one transaction of any status. Order by month ascending, then merchant_id ascending. The pair is unique, so the order is fully determined. All values are whole numbers, so no rounding applies.

Example

transactions

idmerchant_idamountstatusorder_date
110100approved2024-01-03
210250approved2024-01-15
310100chargeback2024-01-20
42080declined2024-01-09
52040declined2024-01-30
61060declined2024-02-02
720300approved2024-02-11
82050chargeback2024-02-27
92070chargeback2024-02-28

Expected result:

monthmerchant_idapproved_amountapproved_countchargeback_amountchargeback_count
2024-011035021001
2024-01200000
2024-02100000
2024-022030011202

Merchant 20 in January and merchant 10 in February have only declined rows, so they appear with zeros. In February, merchant 20's two chargebacks add up to 50 + 70 = 120.

Constraints and Clarifications

  • The table has at most 10,000 rows, and id is the primary key.
  • status is always exactly one of the three lowercase values. No column can be NULL.
  • amount is a positive integer.
  • Declined rows add nothing to any output column, but they make their month and merchant pair appear.
  • A pair with no rows at all does not appear. Missing months are not filled in.
  • Each row counts in the month of its own order_date . A chargeback is not matched back to an earlier approved row.