首页 > 解决方案 > 对聚合上下文中列的引用可能不会出现在聚合函数之外

问题描述

我需要构建一个查询,计算一个类别的每个值(在本例中为策略)在不同时间段的总运行中所占的百分比。我想出了以下查询,但是出现语法错误:

语义错误:在聚合上下文中对列“总计”的引用可能不会出现在聚合函数之外。

requests
| where timestamp between (startTime..endTime)
| extend strategy = tostring(customDimensions.QueryStrategy)
| summarize count() by strategy, bin(timestamp, 30m)
| join (requests 
| where timestamp between (startTime..endTime)
| summarize total = count() by bin(timestamp, 30m)) on timestamp
| project timestamp, strategy, count_, total = toint(total)
| summarize 100 *count()/total by strategy, timestamp

标签: kql

解决方案


这是实现您想要的正确方法:

// The following table literally represents a long calculation
// that ends up with an anonymous tabular value:
datatable (SomeInt:int, SomeSeries:string) [
  100, "Apple",
  200, "Banana",
]
// We now give this calculation a name ("X"):
| as X
// Having this name we can refer to it in the sub-expression
// "X | summarize sum(SomeInt)":
| extend Pct = 100 * bin(todouble(SomeInt) / toscalar(X | summarize sum(SomeInt)), 0.001)

取自https://docs.microsoft.com/en-us/azure/data-explorer/kusto/query/samples?pivots=azuredataexplorer#extend-a-table-by-a-percentage-of-the-total-计算


推荐阅读