我正在努力寻找一种使用SQL对事件进行排名的方法。目标是每当事件比delta
上一次观察超过秒(例如1秒)时,就增加排名。到目前为止,我的尝试如下所示:
select a.event_time, a.user_name, a.object_name, a.rnk, case when a.ddif <= 1000 then 0 else 1 end as new_query,
case when a.ddif <= 1000 then 0 else rnk end as new_rnk
from (
select *, rank() OVER (PARTITION BY user_name ORDER BY event_time) AS rnk,
date_diff('second',lag(event_time) OVER (PARTITION BY user_name ORDER BY event_time),event_time) as ddif
from tmp
) a
但这只给了我以下结果,而我仍然不知道如何获得结果yellow
(它们中的任何一个对我来说都是完美的)。
我将不胜感激。
请注意:我正在使用Presto DB,因此我仅限于此查询引擎。
感谢所有为我指出最终解决方案的方向的好技巧:
select a.*, sum (case when a.ddif <= 1 then 0 else 1 end) over (partition by user_name order by event_time) as acc_rnk
from (
select *, date_diff('second',lag(event_time) OVER (PARTITION BY user_name ORDER BY event_time),event_time) as ddif
from tmp
) a
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句