我有一系列具有(价格,交易时间)属性的市场数据事件。
我想计算每个新市场数据事件的过去时间窗口的简单平均值。简单平均=贸易价格总和/事件数
然而,棘手的部分是我想从当前事件时间计算多个子时间窗口。所以,比如[t-0 min,t-2 min],[t-2 min,t-4 min],[t-4 min,t-6 min]的简单平均值,...
将为每个新事件重新计算这些时间窗口。
现在我只使用多个流并总结[t-0 min,t-2 min],[t-0 min,t-4 min],t-0 min,t-6的价格和事件min],...并通过减法找到他们各自的简单平均值。必须有更好的方法来实现这一点,可能只使用一个或两个流?
答案 0 :(得分:0)
对于任何试图解决同样问题的人:
创建五个交错的时间窗口,其中离开一个窗口的事件进入下一个...
create window W1.win:time(5 min) as select * from yourSource
create window W2.win:time(5 min) as select * from yourSource
create window W3.win:time(5 min) as select * from yourSource
create window W4.win:time(5 min) as select * from yourSource
create window W5.win:time(5 min) as select * from yourSource
insert into W1 select * from yourSource
insert rstream into W2 select rstream * from W1
insert rstream into W3 select rstream * from W2
insert rstream into W4 select rstream * from W3
insert rstream into W5 select rstream * from W4
select sum(price)/count(*) as sma, symbol from W1
GROUP BY symbol
select sum(price)/count(*) as sma, symbol from W2
GROUP BY symbol
select sum(price)/count(*) as sma, symbol from W3
GROUP BY symbol
select sum(price)/count(*) as sma, symbol from W4
GROUP BY symbol
select sum(price)/count(*) as sma, symbol from W5
GROUP BY symbol