You are given a table Purchases that contains information about user purchases with timestamps and amounts.
Problem: Find all users who are eligible for a discount. A user is eligible if they made at least one purchase with amount ≥ minAmount during the time period [startDate, endDate] (inclusive).
Important: The date parameters should be treated as the start of the day (e.g., 2022-03-05 means 2022-03-05 00:00:00).
Return the eligible user IDs ordered by user_id.
Table Schema
| Column Name | Type | Description |
|---|---|---|
user_id
PK
|
int | ID of the user making the purchase |
time_stamp
PK
|
datetime | Timestamp when the purchase was made |
amount
|
int | Amount spent in the purchase |
Input & Output
| user_id | time_stamp | amount |
|---|---|---|
| 1 | 2022-03-10 14:32:00 | 400 |
| 2 | 2022-03-15 09:45:00 | 200 |
| 3 | 2022-03-12 16:20:00 | 500 |
| 1 | 2022-03-25 11:15:00 | 350 |
| user_id |
|---|
| 1 |
| 3 |
Given parameters: startDate = '2022-03-08', endDate = '2022-03-20', minAmount = 300. User 1 has a purchase of 400 on 2022-03-10 (within range and >= 300). User 2 has amount 200 which is below minimum. User 3 has amount 500 on 2022-03-12 (eligible). User 1's purchase on 2022-03-25 is outside the date range.
| user_id | time_stamp | amount |
|---|---|---|
| 1 | 2022-03-10 14:32:00 | 150 |
| 2 | 2022-03-15 09:45:00 | 250 |
| user_id |
|---|
With minAmount = 300, no users meet the minimum amount requirement, so the result is empty.
| user_id | time_stamp | amount |
|---|---|---|
| 1 | 2022-03-08 00:00:00 | 300 |
| 2 | 2022-03-20 23:59:59 | 400 |
| 3 | 2022-03-21 00:00:00 | 500 |
| user_id |
|---|
| 1 |
| 2 |
User 1 and 2 are on the boundary dates and meet criteria. User 3 is one day after the end date, so not eligible.
Constraints
-
1 ≤ user_id ≤ 1000 -
1 ≤ amount ≤ 1000 -
time_stampis a valid datetime - The table may contain multiple purchases per user
-
startDate ≤ endDate -
minAmount ≥ 1