你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Applies to: ✅Microsoft Fabric✅Azure Data Explorer✅Azure Monitor✅Microsoft Sentinel
A let statement is used to set a variable name equal to an expression or a function, or to create views.
let 语句可用于:
- 将复杂表达式分解为多个部分,每个部分由变量表示。
- 定义查询正文外部的常量,以提高可读性。
- 定义变量一次,即可在查询中多次使用。
例如,如果变量以前在嵌套语句中表示另一个值,则会应用最里面的 let 语句。
若要在单个查询中优化 let 语句的多次使用,请参阅优化使用命名表达式的查询。
Note
let 语句将名称绑定到某个计算过程,而不是绑定到该计算过程的计算结果值。 此行为意味着,多次引用同一名称可能会返回不同的值,因为该计算过程已多次参与计算。 If this is not the desired behavior, use toscalar() or materialize().
语法:标量表达式或表格表达式
let
Name=Expression
Important
let 语句必须后跟一个分号。 let 语句之间或 let 语句与其他查询语句之间不能有空行。
Learn more about syntax conventions.
Parameters
| Name | 类型 | Required | Description |
|---|---|---|---|
| Name | string |
✔️ | 变量名称。 可以使用括号对名称进行转义。 例如 ["Name with spaces"]。 |
| Expression | string |
✔️ | 具有标量或表格结果的表达式。 例如,具有标量结果的表达式为 let one=1;,具有表格结果的表达式为 let RecentLog = Logs | where Timestamp > ago(1h)。 |
语法:视图或函数
let
Name= [view] ([ Parameters ]){FunctionBody}
Important
let 语句必须后跟一个分号。 let 语句之间或 let 语句与其他查询语句之间不能有空行。
Learn more about syntax conventions.
Parameters
| Name | 类型 | Required | Description |
|---|---|---|---|
| FunctionBody | string |
✔️ | 生成用户定义函数的表达式。 |
view |
string |
仅与无参数的 let 语句相关。 使用时,let 语句包含在带有 union 运算符的查询中,该运算符具有表/视图的通配符选择。 有关示例,请参阅创建视图或虚拟表。 |
|
| Parameters | string |
零个或多个逗号分隔表格或标量函数参数。 For each parameter of tabular type, the parameter should be in the format TableName :TableSchema, in which TableSchema is either a comma-separated list of columns in the format ColumnName:ColumnType or a wildcard (*). 如果指定了列,则输入表格参数必须包含这些列。 如果指定了通配符,则输入表格参数可以具有任何架构。 若要引用函数正文中的列,必须指定这些列。 有关示例,请参阅带架构的表格参数和带通配符的表格参数。For each parameter of scalar type, provide the parameter name and parameter type in the format Name :Type. The name can appear in the FunctionBody and is bound to a particular value when the user defined function is invoked. 唯一支持的类型是 bool、string、long、datetimetimespan、realdynamic 和这些类型的别名。 |
Note
- 表格参数必须显示在标量参数之前。
- 必须用分号分隔任意两个语句。
Examples
查询示例显示运算符、语句或函数的语法和示例用法。
The examples in this article use publicly available tables in the help cluster, such as the
StormEventstable in the Samples database.
The examples in this article use publicly available tables, such as the
Weathertable in the Weather analytics sample gallery. 可能需要修改示例查询中的表名称以匹配工作区中的表。
定义标量值
下面的示例使用标量表达式语句。
let threshold = 50;
let region = "West";
datatable(Name:string, Score:int, Region:string)
[
"Alice", 45, "West",
"Bob", 60, "East",
"Charlie", 55, "West",
"Dana", 70, "North"
]
| where Score > threshold and Region == region
Output
| Name | Score | Region |
|---|---|---|
| Charlie | 55 | West |
定义表格表达式
以下示例使用 some number 表示法绑定名称 ['name'],然后在表格表达式语句中使用它。
let ['some number'] = 20;
range y from 0 to ['some number'] step 5
Output
| y |
|---|
| 0 |
| 5 |
| 10 |
| 15 |
| 20 |
使用标量计算创建用户定义函数
此示例使用带有自变量的 let 语句进行标量计算。 该查询定义了用于将两个数字相乘的函数 MultiplyByN。
let MultiplyByN = (val:long, n:long) { val * n };
range x from 1 to 5 step 1
| extend result = MultiplyByN(x, 5)
Output
| x | result |
|---|---|
| 1 | 5 |
| 2 | 10 |
| 3 | 15 |
| 4 | 20 |
| 5 | 25 |
创建用于剪裁输入的用户定义函数
以下示例删除输入中的前导/尾随空格。
let TrimOnes = (s:string) { trim("1", s) };
range x from 10 to 15 step 1
| extend result = TrimOnes(tostring(x))
Output
| x | result |
|---|---|
| 10 | 0 |
| 11 | |
| 12 | 2 |
| 13 | 3 |
| 14 | 4 |
| 15 | 5 |
使用多个 let 语句
此示例定义了两个 let 语句,其中一个语句 (foo2) 使用另一个语句 (foo1)。
let foo1 = (_start:long, _end:long, _step:long) { range x from _start to _end step _step};
let foo2 = (_step:long) { foo1(1, 100, _step)};
foo2(2) | count
Output
| result |
|---|
| 50 |
创建视图或虚拟表
此示例演示如何使用 let 语句创建 view 或虚拟表。
let Range10 = view () { range MyColumn from 1 to 10 step 1 };
let Range20 = view () { range MyColumn from 1 to 20 step 1 };
search MyColumn == 5
Output
| $table | MyColumn |
|---|---|
| Range10 | 5 |
| Range20 | 5 |
使用 materialize 函数
materialize() 函数允许在执行查询时缓存子查询结果。 使用 materialize() 函数时,将缓存数据,任何后续的结果调用都使用缓存数据。
let TotalEventsbyLocation = StormEvents
| summarize TotalCount = count() by Location = BeginLocation;
let materializedScope = StormEvents
| summarize by EventType, Location = EndLocation;
let cachedResult = materialize(materializedScope);
cachedResult
| project EventType, Location
| join kind = inner
(
cachedResult
| project EventType, Location
)
on EventType
| where Location != ""
| summarize EventCount = count() by Location
| join kind = inner
TotalEventsbyLocation
on $left.Location == $right.Location
| project Location, EventCount, TotalCount, Percentage = EventCount * 100.0 / TotalCount
Output
| Location | EventCount | TotalCount | Percentage |
|---|---|---|---|
| MELBOURNE BEACH | 112 | 1 | 11,200 |
| EUSTIS | 13,854 | 12 | 115,450 |
| LOTTS | 6,910 | 1 | 691,000 |
| SERVICE | 997 | 1 | 99,700 |
| ... | ... | ... | ... |
使用嵌套的 let 语句
允许使用嵌套的 let 语句,包括在用户定义函数表达式中。 let 语句和自变量适用于函数主体的当前作用域和内部作用域。
let start_time = ago(5h);
let end_time = start_time + 2h;
T | where Time > start_time and Time < end_time | ...
带架构的表格参数
以下示例指定表参数 T 必须有一个类型为 State 的 string 列。 表 T 也可以包含其他列,但不能在函数 StateState 中引用这些列,因为未声明这些列。
let StateState=(T: (State: string)) { T | extend s_s=strcat(State, State) };
StormEvents
| invoke StateState()
| project State, s_s
Output
| State | s_s |
|---|---|
| ATLANTIC SOUTH | 大西洋南大西洋南部 |
| FLORIDA | FLORIDAFLORIDA |
| FLORIDA | FLORIDAFLORIDA |
| GEORGIA | GEORGIAGEORGIA |
| MISSISSIPPI | MISSISSIPPIMISSISSIPPI |
| ... | ... |
带通配符的表格参数
表参数 T 可以具有任何架构,函数 CountRecordsInTable 将正常工作。
let CountRecordsInTable=(T: (*)) { T | count };
StormEvents | invoke CountRecordsInTable()
Output
| Count |
|---|
| 59,066 |