全部评论
with temp1 as (
-- 对每个用户按登陆日期升序编号,如果是连续登陆的话,登陆日期减去对应编号的结果会是一样的。
select qq_no,
ds,
row_number() over(partition by qq_no order by ds asc) as date_diff,
date_sub(ds, row_number() over(partition by qq_no order by ds asc)) as minus_date
from u_wsd.t_od_qidian_pay_hx
where year(ds) = 2019
and month(ds) = 8
),
temp2 as (
-- 找出连续登陆天数大于等于4天的用户,并记录下对应编号的最小值和最大值,只要加上去就可以反推回去这段连续登陆
-- 时间的起始和结束日期
select qq_no,
minus_date,
min(date_diff) as ds_min,
max(date_diff) as ds_max
from temp1
group by qq_no, minus_date
having count(1) >=4
)
select qq_no,
date_add(minus_date,ds_min) as start_date,
date_add(minus_date,ds_max) as end_date
from temp2
;
相关推荐