Table: UserVisits
| Column Name | Type |
|---|---|
| user_id | int |
| visit_date | date |
This table does not have a primary key and might contain duplicate rows. Each row contains the date that a user visited a certain retailer.
Assume today's date is '2021-01-01'.
Write a solution that will, for each user_id, find the largest window of days between each visit and the one right after it (or today if you are considering the last visit).
Return the result table ordered by user_id.
Table Schema
| Column Name | Type | Description |
|---|---|---|
user_id
|
int | ID of the user |
visit_date
|
date | Date when user visited the retailer |
Input & Output
| user_id | visit_date |
|---|---|
| 1 | 2020-11-28 |
| 1 | 2020-10-20 |
| 1 | 2020-12-03 |
| 2 | 2020-10-05 |
| 2 | 2020-12-09 |
| 2 | 2020-12-07 |
| user_id | biggest_window |
|---|---|
| 1 | 39 |
| 2 | 65 |
For user 1: visits on 2020-10-20, 2020-11-28, 2020-12-03. Gaps are 39 days (Oct 20 to Nov 28), 5 days (Nov 28 to Dec 3), and 29 days (Dec 3 to today 2021-01-01). Maximum gap is 39 days.
For user 2: visits on 2020-10-05, 2020-12-07, 2020-12-09. Gaps are 63 days, 2 days, and 23 days to today. Maximum gap is 65 days (Oct 5 to Dec 7).
| user_id | visit_date |
|---|---|
| 1 | 2020-10-05 |
| user_id | biggest_window |
|---|---|
| 1 | 88 |
User 1 has only one visit on 2020-10-05. The only gap is from this visit to today (2021-01-01), which is 88 days.
Constraints
-
1 ≤ user_id ≤ 100 -
visit_dateis a valid date before'2021-01-01' - Table may contain duplicate rows