我正在尝试使用以下查询对 Azure 流分析中具有特定值的结果进行计数:
WITH IoTData AS
( SELECT *,
CAST(iothub.time AS datetime) AS time,
TRY_CAST(iothub.value AS float) AS value,
ref.AssetSignalTag AS assetsignaltag
FROM iothub TIMESTAMP BY iothub.time
JOIN masterdatasql ref
ON ref.[assetsignaltag] = iothub.assetsignaltag
WHERE iothub.value IS NOT NULL
),
HISTORY AS (
SELECT assetsignaltag,
value,
COUNT(*) AS Count
CASE
WHEN value = 1 THEN 1
ELSE 0
END AS ConditionResult,
**LAG(IoTData.value) OVER (PARTITION BY IoTData.AssetSignalTag LIMIT DURATION(hour,1)) AS PreviousValue**
--this lag is used to get rid of duplicate entries coming in every minute
FROM IoTData
)
SELECT
time,
value,
assetsignaltag,
HISTORY.ConditionResult,
HISTORY.Count,
HISTORY.PreviousValue,
**COUNT(*) OVER (PARTITION BY IoTData.assetsignaltag LIMIT DURATION(minute,30) WHEN HISTORY.ConditionResult=1) AS CountThirty,**
--this COUNT should only count when I get a HISTORY.ConditionResult = 1.
AVG(HISTORY.ConditionResultAVG) OVER(PARTITION BY IoTData.assetsignaltag LIMIT DURATION(minute,10)) AS AverageOverTen
INTO eventhub
FROM IoTData
INNER JOIN HISTORY
ON HISTORY.assetsignaltag = IoTData.assetsignaltag
AND DATEDIFF(millisecond, IoTData, HISTORY) BETWEEN 0 AND 500
问题是当资产提交 {0, 1, 0, 1, 0} 时,我得到的不是 2(即提交的 1 的计数)的计数,而是 5,即进来。此外,它将在第二个语句中将 HISTORY.ConditionResult 中的所有值设置为 1,即使它们在 WITH 语句中为 0。
这是 HISTORY 语句中 LAG 函数的结果吗?如果是,我将如何获得真值的计数?