select
DISTINCT b.user_id
from
(select
a.user_id,
date_sub(a.date, interval a.rk DAY) as initial_day
from
(
select
r.user_id,
date(l.log_time) as date,
row_number() over (partition by r.user_id order by l.log_time asc) as rk
from
register_tb r
join login_tb l on r.user_id=l.user_id
) a
) b
group by
b.user_id, b.initial_day
having
count(*) >= 3
order by
b.user_id asc
- 执行顺序:group by->having->select->distinct
- having中可以使用聚合函数,相当于筛选符合条件的组别,对整个组别进行过滤。having不能筛选组内的某几条记录,只能根据聚合后的“整组”结果来保留/丢弃“整个组”

京公网安备 11010502036488号