我正在尝试使用Azure Stream Analytics SQL计算股票价格的开盘价,最低价,最高价和收盘价。
我可以很容易地得到Min和Max,但我很难弄清楚如何计算TumblingWindow的Open(FIRST)和Close(LAST)。
我已经在这里找到了文档(https://docs.microsoft.com/en-us/azure/stream-analytics/stream-analytics-stream-analytics-query-patterns),但我不能将它们组合成一个查询。
这就是我所拥有的:
SELECT
DateAdd(second,-5,System.TimeStamp) as WinStartTime,
system.TimeStamp as WinEndTime,
exchange,
Max(price) as H,
Min(price) as L,
Count(*) as EventCount
FROM [input1]
GROUP BY TumblingWindow(second, 5), exchange
我在SQL方面不是很先进,但有没有办法将它组合成一个查询?即使使用子查询。
注意:5秒只是我选择进行测试的一个示例窗口。
答案 0 :(得分:0)
根据您的方案,我假设您可以利用Azure流分析中的Collect聚合函数和user-defined functions来实现您的目的。以下是详细信息,您可以参考它们:
假设您的输入如下:
[
{"price":1.1,"exchange":10,"Time":"2017-7-24T13:00:00Z"},
{"price":1.2,"exchange":20,"Time":"2017-7-24T13:04:00Z"},
{"price":1.4,"exchange":40,"Time":"2017-7-24T13:03:00Z"},
{"price":1.3,"exchange":30,"Time":"2017-7-24T13:02:00Z"},
{"price":1.5,"exchange":50,"Time":"2017-7-24T13:06:00Z"}
]
<强> UDF 强>
// udf.getLast
function main(objs) {
if(objs==null||objs==undefined||objs.length==0)return null;
return objs[objs.length-1];
}
// udf.getFirst
function main(objs) {
if(objs==null||objs==undefined||objs.length==0)return;
return objs[0];
}
<强> QUERY 强>
SELECT
DateAdd(minute,-5,System.TIMESTAMP) as WinStartTime,
System.TIMESTAMP as WinEndTime,
UDF.getFirst(Collect()) AS FIRST,
UDF.getLast(Collect()) AS LAST,
Max(price) as H,
Min(price) as L,
Count(*) as EventCount
FROM [input1] TIMESTAMP By Time
GROUP BY TumblingWindow(minute, 5)
<强>结果:强>