你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

range 运算符

适用于:✅Azure 数据资源管理器Azure Monitor✅ Sentinel

生成值的单列表。

注意

此运算符不采用表格输入。

语法

range columnNamefrom startto stopstep step

详细了解语法约定

参数

客户 类型​​ 必需 描述
columnName string ✔️ 输出表中的单列名称。
start int、long、real、datetime 或 timespan ✔️ 输出中的最小值。
stop int、long、real、datetime 或 timespan ✔️ 如果 步骤 超过此值,则输出中生成的最高值或值上限。
step int、long、real、datetime 或 timespan ✔️ 两个连续值之间的差异。

注意

这些值不能引用任何表的列。 如需基于输入表计算范围,请使用 range 函数,可能需配合使用 mv-expand 运算符。

返回

一个包含单个列 columnName 的表,其值包括 start、start step...,直至 stop+

示例

本节中的示例演示如何使用语法帮助你入门。

本文中的示例使用 帮助群集中的公开可用表,例如 StormEvents 数据库中的 表。

本文中的示例使用公开可用的表,例如天气分析中的 StormEvents示例数据

过去七天的范围

以下示例创建一个表,其中包含过去七天内延长的当前时间戳的条目,每天一次。

range LastWeek from ago(7d) to now() step 1d

输出

LastWeek
2015-12-05 09:10:04.627
2015-12-06 09:10:04.627
...
2015-12-12 09:10:04.627

合并不同的停止时间

以下示例演示如何使用 union 运算符扩展范围以使用多次停止时间。

let Range1 = range Time from datetime(2024-01-01) to datetime(2024-01-05) step 1d;
let Range2 = range Time from datetime(2024-01-06) to datetime(2024-01-10) step 1d;
union Range1, Range2
| order by Time asc

输出

时间
2024-01-04 00:00:00.0000000
2024-01-05 00:00:00.0000000
2024-01-06 00:00:00.0000000
2024-01-07 00:00:00.0000000
2024-01-08 00:00:00.0000000
2024-01-09 00:00:00.0000000
2024-01-10 00:00:00.0000000

使用参数的范围

以下示例演示如何将 range 运算符与参数配合使用,这些参数随后会作为表进行扩展和使用。

let toUnixTime = (dt:datetime) 
{ 
    (dt - datetime(1970-01-01)) / 1s 
};
let MyMonthStart = startofmonth(now()); //Start of month
let StepBy = 4.534h; //Supported timespans
let nn = 64000; // Row Count parametrized
let MyTimeline = range MyMonthHour from MyMonthStart to now() step StepBy
| extend MyMonthHourinUnixTime = toUnixTime(MyMonthHour), DateOnly = bin(MyMonthHour,1d), TimeOnly = MyMonthHour - bin(MyMonthHour,1d)
; MyTimeline | order by MyMonthHour asc | take nn

输出

MyMonthHour MyMonthHourinUnixTime DateOnly TimeOnly
2023-02-01 00:00:00.0000000 1675209600 2023-02-01 00:00:00.0000000
2023-02-01 04:32:02.4000000 1675225922.4 2023-02-01 00:00:00.0000000
2023-02-01 09:04:04.8000000 1675242244.8 2023-02-01 00:00:00.0000000
2023-02-01 13:36:07.2000000 1675258567.2 2023-02-01 00:00:00.0000000
... ... ... ...

递增步骤

以下示例创建一个表,其中包含一个名为 Steps 的列,其类型为 long,并导致值从 1 到 8 递增 3。

range Steps from 1 to 8 step 3

输出

步骤
1
4
7

时间范围内的跟踪

以下示例演示如何使用 range 运算符创建一个维度表,该表用于在源数据没有值的情况下引入零。 它采用过去四个小时的时间戳,并计算每一分钟间隔的跟踪数。 当特定间隔没有跟踪时,计数为零。

range TIMESTAMP from ago(4h) to now() step 1m
| join kind=fullouter
  (Traces
      | where TIMESTAMP > ago(4h)
      | summarize Count=count() by bin(TIMESTAMP, 1m)
  ) on TIMESTAMP
| project Count=iff(isnull(Count), 0, Count), TIMESTAMP
| render timechart