You are given a transactions table. Find the top 3 customers by total transaction amount over the last 30 days relative to the reference date 2024-06-30, inclusive. That window runs from 2024-06-01 to 2024-06-30. Return customer_id and total_volume, ordered by total_volume descending with ties broken by customer_id ascending. If fewer than 3 customers had transactions in the window, return only those.
Input Table(s)
Table transactions, one row per transaction. Primary key: transaction_id.
| Column | Type | Meaning |
|---|
| transaction_id | INT | Unique transaction id |
| customer_id | INT | Customer who made the transaction |
| amount | DECIMAL(10,2) | Transaction amount, non-negative |
| transaction_date | DATE | Date of the transaction |
Output Contract
-
Columns, in order:
customer_id
,
total_volume
.
-
total_volume
is the sum of
amount
over the customer's transactions dated from
2024-06-01
to
2024-06-30
, both included. It is an exact decimal with 2 places. Do not round it further.
-
Rows are ordered by
total_volume
descending, then
customer_id
ascending.
-
Return at most 3 rows.
Example
Input transactions:
| transaction_id | customer_id | amount | transaction_date |
|---|
| 1 | 101 | 120.00 | 2024-06-01 |
| 2 | 102 | 80.50 | 2024-06-10 |
| 3 | 101 | 30.00 | 2024-06-15 |
| 4 | 103 | 95.25 | 2024-06-20 |
| 5 | 104 | 95.25 | 2024-06-30 |
| 6 | 102 | 500.00 | 2024-05-31 |
| 7 | 105 | 300.00 | 2024-07-01 |
| 8 | 102 | 19.50 | 2024-06-25 |
Expected output:
| customer_id | total_volume |
|---|
| 101 | 150.00 |
| 102 | 100.00 |
| 103 | 95.25 |
Row 6 (2024-05-31) and row 7 (2024-07-01) fall outside the window, so customer 102 totals 80.50 + 19.50 = 100.00 and customer 105 has nothing in range. Rows 1 and 5 sit exactly on the window's first and last day and both count. Customers 103 and 104 tie at 95.25 for third place, and 103 takes the slot because its id is smaller.
Constraints and Clarifications
-
transactions
has 1 to 100,000 rows.
-
amount
is non-negative with up to 2 decimal places, and
transaction_date
is a valid DATE. No column is NULL.
-
Rows dated before
2024-06-01
or after
2024-06-30
do not count. That includes rows dated after the reference date.
-
A customer can have many transactions in the window. Sum all of them.
-
Customers with no transactions in the window do not appear in the output.
-
When customers tie on
total_volume
, the smaller
customer_id
ranks higher, so a tie at third place still yields exactly 3 rows.