【发布时间】:2020-05-08 09:43:16
【问题描述】:
我正在处理一些包含play 和end 的播放状态日志数据。在游戏过程中,客户端会多次报告play,但当出现end时,表示游戏结束,比如
user_id status timestamp
a play xxx
a play xxx
a play xxx
a end xxx
b play xxx
b end xxx
c play xxx
c play xxx
c end xxx
a play xxx
a play xxx
a end xxx
a play xxx
a end xxx
现在,我可以使用row_number() 来计算user_id 的数量与超过2 个play 状态的游戏类似:
# I realize this is a wrong query...
select count(distinct user_id) as cnt_uid
(select
user_id,status,timestamp,
row_number() over (partition by user_id, status, order by timestamp) as rn
from tableA) a
where rn>=2
但是如果我需要计算超过 2 个play 状态的游戏数量(例如:user A 有 2 个游戏超过 2 个play 状态,user C 有 1 个),该怎么做?任何帮助表示赞赏。
*PS:预期结果只是一些游戏的状态超过 2 个play,对于上面给出的数据,结果是3。
【问题讨论】:
-
如果在 hive 中可用,您可以使用 DENSE_RANK()
-
@DigvijayS 感谢您的回复。我在
Hive中尝试了dense_rank() over (order by status),我认为结果等同于user_id...如果我没有以正确的方式使用它,请随时告诉我.... -
您能否通过编辑您的帖子添加预期输出。
-
嗨@DigvijayS 我刚刚修改了帖子,实际上,这种情况下的预期输出只是一个数字:
3