题解|26. 单日登录次数大于等于2次的用户数
单日登录次数大于等于2次的用户数
明确题意:
统计单日登录次数大于等于2次的用户数
问题拆解:
- 统计单日登录次数。知识点:按用户ID和日期分组group by;统计登录次数count
- 筛选登录次数大于等于2的用户。知识点:having
- 统计满足条件的用户个数。知识点:count(distinct)
代码实现:
select count(distinct user_id) as num
from (
select user_id, login_date, count(1) as login_times
from logintb
group by user_id, login_date
having count(login_times)>=2
) as t_login_times
