目录
题目地址
https://leetcode.cn/problems/active-users/description/
题目描述
活跃用户 是指那些至少连续 5 天登录账户的用户。
编写解决方案, 找到 活跃用户 的 id 和 name。
返回的结果表按照 id
排序 。
代码
注意需要处理,同一天多次登录的情形。
# Write your MySQL query statement below
WITH t1 AS (
SELECT
#------------只需要修改里面的逻辑就行。注意语句是“顺序执行”的-------------------------------
login_date,
id,
(case when login_date = @prev_date + INTERVAL 1 DAY AND id = @prev_author_id then @consec_days := @consec_days + 1
when login_date = @prev_date AND id = @prev_author_id then @consec_days := @consec_days
else @consec_days := 1 end)
AS consec_days,
@prev_date := login_date,
@prev_author_id := id
#-------------只需要修改里面的逻辑就行。注意语句是“顺序执行”的------------------------------
FROM
(SELECT @prev_date := NULL, @prev_author_id := NULL, @consec_days := 1) vars,
(SELECT login_date, id FROM Logins ORDER BY id , login_date) ordered_dates
)
select a.* from
(SELECT distinct id
from t1
where consec_days>=5) tmp1
join Accounts a on a.id=tmp1.id
order by a.id asc
标签:自定义,days,用户,LeetCode1454,date,login,prev,id,consec
From: https://www.cnblogs.com/yhm138/p/17642102.html