Hive综合案例练习(中级)第二十二题:查询相同时刻多地登陆的用户

查询相同时刻多地登陆的用户

题目需求

从登录明细表(user_login_detail)中查询在相同时刻,多地登陆(ip_address不同)的用户,期望结果如下:

user_id(用户id)
101
102

代码实现

select
  distinct t2.user_id
from
  (
   select
     t1.user_id,
     if(t1.max_logout is null ,2,if(t1.max_logout<t1.login_ts,1,0)) flag
   from
     (
      select
        user_id,
        login_ts,
        logout_ts,
        max(logout_ts) over (partition by user_id order by login_ts rows between unbounded preceding and 1 preceding) max_logout
      from
        user_login_detail
   )t1
)t2
where
  t2.flag=0

你可能感兴趣的:(Hive综合案例练习,hivesql)