hive计算连续登陆三天的用户的代码
时间: 2024-05-08 16:17:36 浏览: 157
hive-json-schema最新源代码
以下是计算连续登陆三天的用户的Hive SQL代码:
```
SELECT DISTINCT user_id
FROM (
SELECT user_id,
date,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY date) AS row_num
FROM login_table
) AS t1
JOIN (
SELECT user_id,
date,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY date) AS row_num
FROM login_table
) AS t2 ON t1.user_id = t2.user_id AND t1.row_num = t2.row_num - 1 AND t1.date = DATE_SUB(t2.date, 1)
JOIN (
SELECT user_id,
date,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY date) AS row_num
FROM login_table
) AS t3 ON t1.user_id = t3.user_id AND t1.row_num = t3.row_num - 2 AND t1.date = DATE_SUB(t3.date, 2)
```
这个查询使用了三个子查询,分别对应用户的每次登录记录,并使用 ROW_NUMBER() 函数为每个用户的每次登录记录分配一个行号。然后,我们对这三个子查询进行了自连接,以找到连续的三次登录记录。最后,我们使用 DISTINCT 关键字来获取唯一的用户ID。
阅读全文