问题描述
在 Azure Stream Analytic(IoT 中心)中,无论我指定什么时间窗口,TumblingWindow 函数都无法比较时间并且不会返回我需要的窗口。我正在尝试使用下面的 sql 代码块返回一个 2 秒的窗口,但输出包括所有事件。由于 Stream Analytic 中没有枢轴函数,我使用的是 @Joe-Zhang
建议的方法在这种情况下,每 2 秒有一个 IoT 读取事件,我期望只返回一个事件 -
with tempone as (
select
cast(dataArr.ArrayValue.sourceTimestamp as datetime) as SourceTimestamp,cast(valuesArr.ArrayValue.Address as bigint) as Address2,max(cast(valuesArr.ArrayValue.Value as float)) as Value2
from iotinput i
cross apply GetArrayElements(i.Content) as contentArr
cross apply GetArrayElements(contentArr.ArrayValue.Data) as dataArr
cross apply GetArrayElements(dataArr.ArrayValue.[Values]) as valuesArr
WHERE cast(valuesArr.ArrayValue.Address as bigint) = 30002
GROUP BY cast(dataArr.ArrayValue.sourceTimestamp as datetime),cast(valuesArr.ArrayValue.Address as bigint),TumblingWindow(second,2)
),temptwo AS (
select
cast(dataArr.ArrayValue.sourceTimestamp as datetime) as SourceTimestamp,cast(valuesArr.ArrayValue.Address as bigint) as Address3,max(cast(valuesArr.ArrayValue.Value as float)) as Value3
from iotinput i
cross apply GetArrayElements(i.Content) as contentArr
cross apply GetArrayElements(contentArr.ArrayValue.Data) as dataArr
cross apply GetArrayElements(dataArr.ArrayValue.[Values]) as valuesArr
WHERE cast(valuesArr.ArrayValue.Address as bigint) = 30003
GROUP BY cast(dataArr.ArrayValue.sourceTimestamp as datetime),2)
)
select tempone.sourceTimestamp,tempone.Value2 as Temperature,temptwo.Value3 as Humidity from tempone
join temptwo on tempone.sourceTimestamp = temptwo.sourceTimestamp
and DATEDIFF(second,tempone,temptwo) BETWEEN 0 AND 2
返回值 -
解决方法
如果您不使用 TIMESTAMP BY,时间逻辑将基于摄取时间。
在这里,您似乎希望在 SourceTimestamp 上完成时间处理,但您没有对它进行 TIMESTAMP BY。