Kusto 子查询引用"outer"查询



我正在尝试编写一个Kusto(KQL(查询,在SQL中,我会使用引用";外部";查询如下。然而,我无法找到/理解如何在KQL中实现等效功能。是否可以有一个子查询引用";外部";KQL中的查询?如果没有,请使用其他方法来完成以下操作?

基本思想是将一个表连接到其自身上,以获得下一个时间值。IE返回最小时间值,其中时间大于当前记录时间并与密钥匹配(在这种情况下为DeviceName(。

DECLARE @DeviceIPs TABLE (DeviceName NVARCHAR(MAX), DeviceIP NVARCHAR(MAX), TimeGenerated DATETIME)
INSERT INTO @DeviceIPs SELECT 'PC1', '192.168.100.1', '2021-01-01'
INSERT INTO @DeviceIPs SELECT 'PC1', '192.168.100.2', '2021-01-02'
INSERT INTO @DeviceIPs SELECT 'PC1', '192.168.100.3', '2021-01-03'
INSERT INTO @DeviceIPs SELECT 'PC2', '192.168.100.3', '2021-01-01'
INSERT INTO @DeviceIPs SELECT 'PC2', '192.168.100.1', '2021-01-02'
INSERT INTO @DeviceIPs SELECT 'PC2', '192.168.100.2', '2021-01-03'
SELECT
i.DeviceName,
i.DeviceIP,
i.TimeGenerated AS BeginDateTime,
ISNULL((SELECT MIN(i2.TimeGenerated) FROM @DeviceIPs i2 WHERE i.DeviceName = i2.DeviceName AND i.TimeGenerated < i2.TimeGenerated), '2200-01-01') AS EndDateTime
FROM
@DeviceIPs i

在上述数据结构中,每一行表示设备被授予IP地址的时间(BeginDateTime(。我正在寻找的结果是;结束日期时间";这将是该设备下一次被授予IP地址。如果不存在";下一个记录";,我只是设定一些随机的未来日期作为结束日期,但这部分与这个问题并不相关。预期结果是:

开始日期时间结束日期时间
DeviceNameDeviceIP
PC1192.168.0.12021-01-012021:01-02
PC1192.168.0.22021-01-022021:01-03
PC1192.168.0.32021-01-032200-01-01
PC2192.168.0.32021-01-012021:01-02
PC2192.168.0.12021-01-022021:01-03
PC2192.168.0.22021-01-032200-01-01

假设我正确理解了你的意图,以下内容应该有效:

.create table DeviceIPs (DeviceName:string, DeviceIP: string, TimeGenerated:datetime)
.ingest inline into table DeviceIPs <|
PC1,192.168.100.1,2021-01-01
PC1,192.168.100.2,2021-01-02
PC1,192.168.100.3,2021-01-03
PC2,192.168.100.3,2021-01-01
PC2,192.168.100.1,2021-01-02
PC2,192.168.100.2,2021-01-03
DeviceIPs
| order by DeviceName asc, TimeGenerated asc
| project DeviceName, DeviceIP, BeginDateTime = TimeGenerated, EndDateTime = case(next(DeviceName) == DeviceName, next(TimeGenerated), datetime(2200-01-01))
开始日期时间结束日期时间2021:01-03 00:00:00.0000000002200-01-01 00:00:00.000000000
DeviceNameDeviceIP
PC1192.168.100.12021-01-01 00:00:00.0000000000
PC1192.168.100.22021-01-02 00:00:00.0000000000
PC1192.168.100.32021-01-03 00:00:00.00000000002200-01-01 00:00:00.000000000
PC2192.168.100.32021-01-01 00:00:00.00000000002021:01-02 00:00:00.000000000
PC2192.168.100.12021-01-02 00:00:00.00000000002021:01-03 00:00:00.000000000
PC2192.168.100.22021-01-03 00:00:00.0000000000

最新更新