Find the top 3 merchants by total spend for the user with user_id = 1, counting only transactions where transaction_date >= '2024-01-01' and transaction_date < '2024-01-31'. The upper bound is exclusive, so the window runs from January 1 through January 30, 2024, and transactions dated January 31 are not counted. Return each merchant's merchant_name and total_spend, the sum of amount over those transactions.
Input Table(s)
transactions
| Column | Type | Meaning |
|---|
| transaction_id | INT | Transaction identifier. Primary key. |
| user_id | INT | User who made the transaction. |
| merchant_id | INT | Merchant the money was spent at. Foreign key to merchants.merchant_id. |
| amount | DECIMAL(10,2) | Amount spent. Always positive. |
| transaction_date | DATE | Date of the transaction. |
Keys: transaction_id is the primary key, and merchant_id references merchants.merchant_id.
merchants
| Column | Type | Meaning |
|---|
| merchant_id | INT | Merchant identifier. Primary key. |
| merchant_name | VARCHAR(100) | Merchant name. No uniqueness constraint. |
Key: merchant_id is the primary key.
Output Contract
| Column | Meaning |
|---|
| merchant_name | The merchant's name. |
| total_spend | Sum of amount over user 1's transactions at that merchant inside the window. No rounding is needed, since amount already has 2 decimal places. |
Order the rows by total_spend descending, then by merchant_id ascending, and return only the first 3 rows. merchant_id is used for ordering but is not an output column. If fewer than 3 merchants qualify, return all of them.
Example
merchants
| merchant_id | merchant_name |
|---|
| 10 | Coffee Corner |
| 20 | Book Barn |
| 30 | Fuel Stop |
| 40 | Grocer Hub |
| 50 | Movie House |
transactions
| transaction_id | user_id | merchant_id | amount | transaction_date |
|---|
| 1 | 1 | 10 | 12.50 | 2024-01-03 |
| 2 | 1 | 10 | 7.50 | 2024-01-15 |
| 3 | 1 | 20 | 45.00 | 2024-01-10 |
| 4 | 1 | 30 | 20.00 | 2024-01-30 |
| 5 | 1 | 40 | 15.00 | 2024-01-01 |
| 6 | 1 | 40 | 5.00 | 2024-01-20 |
| 7 | 1 | 40 | 100.00 | 2024-01-31 |
| 8 | 2 | 50 | 300.00 | 2024-01-12 |
| 9 | 1 | 50 | 80.00 | 2023-12-31 |
Expected output:
| merchant_name | total_spend |
|---|
| Book Barn | 45.00 |
| Coffee Corner | 20.00 |
| Fuel Stop | 20.00 |
Transaction 7 is dated January 31, transaction 8 belongs to user 2 and transaction 9 is from 2023, so none of them count. User 1's totals are 45.00 at Book Barn and 20.00 each at Coffee Corner (12.50 + 7.50), Fuel Stop and Grocer Hub (15.00 + 5.00). The three-way tie at 20.00 is broken by merchant_id, so merchants 10 and 30 make the cut and Grocer Hub (40) does not.
Constraints and Clarifications
-
transactions
has at most 10,000 rows and
merchants
has at most 1,000 rows.
-
amount
is always positive, so it is never NULL.
transaction_date
is always a valid DATE.
-
At least one transaction exists for
user_id = 1
in the window, so the result has at least one row.
-
Every
merchant_id
in
transactions
has a matching row in
merchants
.
-
Ties on
total_spend
are broken by
merchant_id
ascending. The same rule decides which merchant is kept when the third and fourth totals are equal, and the result never has more than 3 rows.
-
Two merchants can share a
merchant_name
. They are still different merchants with separate totals.