我想计算存在于当前行和前x行(滑动窗口)之间的不同端口号的数量,其中x可以是任何整数。
例如,
如果输入为:
ID PORT
1 21
2 22
3 23
4 25
5 25
6 21
输出应为:
ID PORT COUNT
1 21 1
2 22 2
3 23 3
4 25 4
5 25 4
6 21 4
我正在rapidminer上使用hive,我尝试了以下方法:
select id, port,
count (*) over (partition by srcport order by id rows between 5 preceding and current row)
这必须适用于大数据,当x是大整数时。
任何反馈都将不胜感激。
2条答案
按热度按时间dldeef671#
一种方法是使用
self join
作为distinct
在窗口函数中不支持。这假设id是按顺序排列的。
如果不是,首先得到行号并使用上面的逻辑。就像
enyaitl32#
我不认为有一个简单的方法。一种方法使用
lag()
:这是一个复杂的查询,但性能应该还可以。
注:
port
以及srcport
我假设是一样的,但这是借用你的查询。