Hive sql实现查询连续n天登录的用户

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-28 19:45   41   0

用个排序的窗口函数即可实现:

select distinct user_id
from (
    select user_id
        , continue_date
        , count(login_date) as continue_day_cnt
    from (
        select user_id
            , login_date
            , login_date - row_number() over(partition by user_id order by login_date) as continue_date
        from (
            select user_id
                , substr(login_time, 1, 10) as login_date
            from dw.dw_user_login_log
            where 1 = 1
            group by user_id
                , substr(login_time, 1, 10)
        ) t
    ) m
    where 1 = 1
    group by user_id
        , continue_date
    having count(login_date) >= 3  -- 连续3天登录,根据业务场景调整这一数值
) x ;

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP