流分析查询

时间:2017-03-08 10:14:53

标签: azure-stream-analytics

我的要求

我想计算汽车进入隧道的时间 为此,我有JSON(我已将此JSON填充到Stream Analytics输入作业中)

[{
    CarId: 1,
    Time: "2017-03-08T16:20:00.0000000",
    CarNum: "XYZ"
},{
    CarId: 2,
    Time: "2017-03-08T16:25:00.0000000",
    CarNum: "ABC"
},{
    CarId: 1,
    Time: "2017-03-08T16:27:00.0000000",
    CarNum: "XYZ"
},{
    CarId: 1,
    Time: "2017-03-08T16:30:00.0000000",
    CarNum: "XYZ"
},{
    CarId: 1,
    Time: "2017-03-08T16:35:00.0000000",
    CarNum: "XYZ"
},
]

现在汽车进入隧道的最短时间是1分钟,最长时间是10分钟。

根据输入,CarId 1为2次访问,CarId 2为0次访问

有人可以帮我写这个查询吗?

1 个答案:

答案 0 :(得分:1)

在您的示例数据中,CarId&的组合似乎重复了CarNum。基于流分析查询语言(它只是T-SQL的一个子集),您所需的查询几乎不会实现。

根据我的经验,这是我与您的类似方案的示例查询。

如果json数据如下,

[{
    CarId: 1,
    Time: "2017-03-08T16:20:00.0000000",
    CarNum: "XYZ"
},{
    CarId: 2,
    Time: "2017-03-08T16:25:00.0000000",
    CarNum: "ABC"
},{
    CarId: 1,
    Time: "2017-03-08T16:27:00.0000000",
    CarNum: "XYZ"
},{
    CarId: 1,
    Time: "2017-03-08T16:30:00.0000000",
    CarNum: "DEF"
},{
    CarId: 1,
    Time: "2017-03-08T16:35:00.0000000",
    CarNum: "DEF"
},
]

CarId&的组合CarNum是唯一的在一段时间内,Stream Analytics的查询如下所示。

SELECT
    CarId,
    CarNum,
    LAG(Time, 1) OVER (PARTITION BY CarId, CarNum LIMIT DURATION(MINUTE, 10)) AS startime,
    Time AS endtime,
    DATEDIFF(second, LAG(Time, 1) OVER (PARTITION BY CarId, CarNum LIMIT DURATION(MINUTE, 10)), Time) AS time
FROM
    [YourInputAlias]
WHERE LAG(CarId, 1) OVER (PARTITION BY CarId, CarNum limit LIMIT DURATION(MINUTE, 10)) IS NOT NULL