You are given a table Users containing purchase records. Your task is to identify active users.
An active user is defined as a user who has made a second purchase within 7 days of any other of their purchases.
Important: "Within 7 days" means if one purchase is on May 31, 2023, then any purchase between May 31, 2023, and June 7, 2023 (inclusive) qualifies as within 7 days.
Return a list of user_id values representing the active users in any order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
user_id
PK
|
int | User identifier |
item
|
varchar | Name of purchased item |
created_at
PK
|
datetime | Date and time of purchase |
amount
|
int | Purchase amount in cents |
Input & Output
| user_id | item | created_at | amount |
|---|---|---|---|
| 1 | Book | 2023-05-01 | 1500 |
| 1 | Phone | 2023-05-05 | 50000 |
| 2 | Laptop | 2023-05-01 | 80000 |
| 2 | Mouse | 2023-05-15 | 2500 |
| user_id |
|---|
| 1 |
User 1: Made purchases on 2023-05-01 and 2023-05-05. The second purchase is 4 days after the first, which is within 7 days. Therefore, User 1 is active.
User 2: Made purchases on 2023-05-01 and 2023-05-15. The second purchase is 14 days after the first, which exceeds 7 days. Therefore, User 2 is not active.
| user_id | item | created_at | amount |
|---|---|---|---|
| 3 | Tablet | 2023-06-01 | 30000 |
| 3 | Case | 2023-06-07 | 1000 |
| 4 | Monitor | 2023-06-10 | 25000 |
| 4 | Keyboard | 2023-06-12 | 5000 |
| 5 | Headphones | 2023-06-01 | 8000 |
| user_id |
|---|
| 3 |
| 4 |
User 3: Purchased on 2023-06-01 and 2023-06-07 (exactly 6 days apart, within 7 days). Active user.
User 4: Purchased on 2023-06-10 and 2023-06-12 (2 days apart, within 7 days). Active user.
User 5: Only made one purchase, so cannot be considered active.
| user_id | item | created_at | amount |
|---|---|---|---|
| 6 | Watch | 2023-07-01 | 15000 |
| 6 | Band | 2023-07-08 | 2000 |
| 7 | Camera | 2023-07-01 | 40000 |
| 7 | Lens | 2023-07-09 | 20000 |
| user_id |
|---|
User 6: Purchased on 2023-07-01 and 2023-07-08 (7 days apart, but this is 8 days since we count inclusively: July 1→8 is 8 days). Not active.
User 7: Purchased on 2023-07-01 and 2023-07-09 (8 days apart, exceeds 7 days). Not active.
No users qualify as active in this example.
Constraints
-
1 ≤ user_id ≤ 10000 -
itemis a non-empty string -
created_atis a valid datetime -
1 ≤ amount ≤ 100000 - The table may contain duplicate records