表:UserVisits
Column Name | Type |
---|---|
user_id | int |
visit_date | date |
假设今天的日期是 ‘2021-1-1’ 。
编写解决方案,对于每个 user_id ,求出每次访问及其下一个访问(若该次访问是最后一次,则为今天)之间最大的空档期天数 window 。
返回结果表,按用户编号 user_id 排序。
结果格式如下示例所示:
示例 1:
输入:
UserVisits 表:
user_id | visit_date |
---|---|
1 | 2020-11-28 |
1 | 2020-10-20 |
1 | 2020-12-3 |
2 | 2020-10-5 |
2 | 2020-12-9 |
3 | 2020-11-11 |
输出:
user_id | biggest_window |
---|---|
1 | 39 |
2 | 65 |
3 | 51 |
解释:
对于第一个用户,问题中的空档期在以下日期之间:
由此得出,最大的空档期为 39 天。
对于第二个用户,问题中的空档期在以下日期之间:
由此得出,最大的空档期为 65 天。
对于第三个用户,问题中的唯一空档期在 2020-11-11 至 2021-1-1 之间,共计 51 天。
SELECT user_id, MAX(biggest_window) AS biggest_window
FROM (
SELECT *, DATEDIFF(
IFNULL(
Lead(visit_date) over (partition by user_id order by visit_date),
'2021-1-1'
), visit_date
) AS biggest_window
FROM UserVisits
) AS one
GROUP BY user_id
如果加 IFNULL
SELECT *, DATEDIFF(
IFNULL(
Lead(visit_date) over (partition by user_id order by visit_date),
'2021-1-1'
), visit_date
) AS biggest_window
FROM UserVisits
| user_id | visit_date | biggest_window |
| ------- | ---------- | -------------- |
| 1 | 2020-10-20 | 39 |
| 1 | 2020-11-28 | 5 |
| 1 | 2020-12-03 | 29 |
| 2 | 2020-10-05 | 65 |
| 2 | 2020-12-09 | 23 |
| 3 | 2020-11-11 | 51 |
如果不加 IFNULL,还需要特别处理
SELECT *, DATEDIFF(
Lead(visit_date) over (partition by user_id order by visit_date),
visit_date
) AS biggest_window
FROM UserVisits
| user_id | visit_date | biggest_window |
| ------- | ---------- | -------------- |
| 1 | 2020-10-20 | 39 |
| 1 | 2020-11-28 | 5 |
| 1 | 2020-12-03 | null |
| 2 | 2020-10-05 | 65 |
| 2 | 2020-12-09 | null |
| 3 | 2020-11-11 | null |
SELECT user_id, MAX(biggest_window) AS biggest_window
FROM (
SELECT *, DATEDIFF(
Lead(visit_date, 1, '2021-01-01') over (partition by user_id order by visit_date),
visit_date
) AS biggest_window
FROM UserVisits
) AS one
GROUP BY user_id