【发布时间】:2020-05-12 23:12:21
【问题描述】:
我正在努力寻找一种使用 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,因此我仅限于这个查询引擎。
【问题讨论】:
标签: sql window-functions presto gaps-and-islands