我有以下查询,它结合了来自两个输入的事件。
WITH combined AS (
SELECT a.deviceId, temperature, 'a' as source FROM [inputA] a timestamp by a.devicetime
UNION
SELECT b.deviceId, temperature, 'b' as source FROM [inputB] b timestamp by b.devicetime
)
SELECT c.deviceId, system.Timestamp as 'windowend', avg(c.temperature) as 'avg_temperature'
INTO [ehA-output]
FROM combined c
GROUP BY c.deviceId, TumblingWindow(Duration(second, 10), Offset(second, 1))
我还没有弄清楚的是:如何将字段“源”添加到第二部分的输出中,而该值应取自窗口组中的最后一个事件?
所以我想像伪代码(注意:LAST()实际上是一个现有函数,但据我所知并不是为了这个目的)。
SELECT c.deviceId, ..., LAST(source) as sourceOfLastEvent
...
答案 0 :(得分:1)
尝试以下查询(请调整您的时间范围,因为我出于测试目的对其进行了更改):
WITH combined AS (
SELECT a.deviceId, temperature, 'a' as source, a.deviceTime FROM [input] a timestamp by a.devicetime
UNION
SELECT b.deviceId, temperature, 'b' as source, b.deviceTime FROM [input1] b timestamp by b.devicetime
),
result AS (
SELECT c.deviceId, system.Timestamp as 'windowend', avg(c.temperature) as 'avg_temperature', topone() over (order by deviceTime desc)
FROM combined c
GROUP BY c.deviceId, TumblingWindow(Duration(minute, 1), Offset(second, 1))
)
select result.topone.source, result. *
into output
from result
在这里您可以看到,在第一个子查询中,传播了 deviceTime ,在第二个子查询中,我们采用了按设备时间降序排列的 topone 元素。
这应该从时间窗口获取最后一个事件,但不会改变GROUP BY子句,因为 topone ()函数是一个聚合表达式。
最后,从 topone 对象得到的结果中,我们只是将source属性传播到输出中。
旁注:根据我的测试,似乎UNION子句在此处要求源'a'和源'b'都具有事件才能产生输出,这对于您可能至关重要对这些事件有实时要求,例如,只有输入源“ a”正在获取事件。