说我在雅典娜有以下数据:
id ts
uid1 1499672134268
uid1 1499672134292
uid1 1499672136189
uid1 1499672136212
uid1 1499719927907
uid1 1499719927940
uid1 1499719927975
uid1 1499719927999
uid2 1499670000000
uid2 1499670000010
uid2 1499688880010
我想这样排名:
rank id ts
1 uid1 1499672134268
1 uid1 1499672134292
1 uid1 1499672136189
1 uid1 1499672136212
2 uid1 1499719927907
2 uid1 1499719927940
2 uid1 1499719927975
2 uid1 1499719927999
1 uid2 1499670000000
1 uid2 1499670000010
2 uid2 1499688880010
逻辑是:
第二组最小值与第一组最大值之差大于x
我认为dense_rank() OVER (PARTITION BY id ... )
的某种组合可以解决它,但我完全是SQL中窗口函数的新手。
谢谢
您可以使用lag
并获取与上一行ts
的差异,并在它> x 时使用运行总和重置它。
select id,ts,1+sum(col) over(partition by id order by ts) as rnk
from (select id,ts
,case when ts-lag(ts,1,ts) over(partition by id order by ts) > 3000 then 1 else 0 end as col
from tbl
) t
将case
表达式中的3000
(x( 替换为所需的值。