For each calendar month that has at least one row in Transactions, report the month as a YYYY-MM string, the total revenue for that month (the sum of amount), and the number of distinct customers who made at least one purchase that month. Return the months in ascending order.
Input Table(s)
Transactions
| Column | Type | Meaning |
|---|
transaction_id | INT | Primary key, one row per transaction |
customer_id | INT | Customer who made the purchase, never NULL |
transaction_date | DATE | Date of the purchase, never NULL |
amount | DECIMAL(10,2) | Purchase amount, never NULL and never negative |
Primary key: transaction_id. A customer can have many transactions, including several in the same month or on the same day.
Output Contract
| Column | Type | Meaning |
|---|
month | TEXT | Year and month in YYYY-MM format, for example 2024-01 |
total_revenue | DECIMAL | Sum of amount over the month's transactions, kept at two decimal places with no extra rounding |
unique_customers | INTEGER | Count of distinct customer_id values with a transaction that month |
Return one row per month that appears in the data, ordered by month ascending. month is unique in the output, so this order is fully determined.
Example
Input Transactions:
| transaction_id | customer_id | transaction_date | amount |
|---|
| 1 | 101 | 2023-12-30 | 15.00 |
| 2 | 101 | 2024-01-05 | 50.00 |
| 3 | 102 | 2024-01-17 | 30.00 |
| 4 | 101 | 2024-01-28 | 20.00 |
| 5 | 103 | 2024-02-02 | 75.50 |
| 6 | 103 | 2024-02-20 | 0.00 |
Expected output:
| month | total_revenue | unique_customers |
|---|
| 2023-12 | 15.00 | 1 |
| 2024-01 | 100.00 | 2 |
| 2024-02 | 75.50 | 1 |
Customer 101 bought twice in January 2024 and counts once for that month, and also counts in December 2023. January revenue is 50.00 + 30.00 + 20.00 = 100.00. Customer 103's zero-amount purchase still counts as a purchase in February.
Constraints and Clarifications
-
The table holds 1 to 100,000 transactions.
-
transaction_id
is unique, and no column contains NULL.
-
amount
is a non-negative decimal, and a transaction with
amount
of 0 still counts toward
unique_customers
.
-
Months with no transactions do not appear in the output. Do not generate empty months.
-
The same month in different years forms separate groups: January 2023 and January 2024 are two rows.