有没有办法在Kusto语言的不同列中使用where clausule。我知道“Pivot”语法也与SQL一起用于基于唯一值创建列。但是不要认为这对我的情况有帮助。还有另一个SO question几乎和我有同样的问题。但是他的解决方案也没有用。
我的查询的上下文:此查询获取每个月的每台计算机的运行时。您可能想知道为什么我使用这么长的查询来实现这一点。欢迎任何意见和调整。我对这门语言很新。我已经使用top查询来获取另一个项目中每个VM的启动和停止时间。
原始查询:
AzureActivity
| where ResourceProvider == "Microsoft.Compute"
and ActivityStatus == "Succeeded"
and OperationName == "Deallocate Virtual Machine"
| project DeallocateResource=Resource
,DeallocatedDate=format_datetime(EventSubmissionTimestamp, 'yyyy-MM-dd')
,DeallocatedTime=format_datetime(EventSubmissionTimestamp, 'HH:mm:ss')
| join kind=fullouter (AzureActivity
| where ResourceProvider == "Microsoft.Compute"
and ActivityStatus == "Succeeded"
and OperationName == "Start Virtual Machine"
| project StartupResource=Resource
,StartDate=format_datetime(EventSubmissionTimestamp, 'yyyy-MM-dd')
,StartTime=format_datetime(EventSubmissionTimestamp, 'HH:mm:ss')
) on $right.StartupResource == $left.DeallocateResource
| where StartDate == DeallocatedDate
| project Resource=coalesce(StartupResource, DeallocateResource) ,
Runtime = round(todouble(datetime_diff('minute', todatetime(strcat(StartDate , " " , DeallocatedTime )) , todatetime(strcat(StartDate , " " , StartTime )))) / 60)
| summarize sum(Runtime) by Resource
现在,上面的查询将获得运行时间与您在门户中专门设置的时间范围的总和。要获得每个月的运行时间总和(日志分析设置为90天,因此3个月前)我将这些语句添加到where。在3个不同的查询中。工作完成了我有3个不同的表,每个月的运行时间是(month1,month2,month3)。
| where TimeGenerated > ago(30d)
| where TimeGenerated between(ago(30d) .. ago(60d) )
| where TimeGenerated between(ago(60d) .. ago(90d) )
但这些是3个不同的查询和3个不同的表。我的目标是让你看看你有3个不同的地方(在一个表里面生成时间的time)
试过SO问题解决方案,但没有按计划进行(在我的原始查询中添加这些代码行时,无法解析名为'TimeGenerated'错误的标量表达式)
| summarize sum(Runtime) by Resource , bin(TimeGenerated, 1m)
| summarize Fistmonth = TimeGenerated > ago(30d),
SecondMonth = TimeGenerated between(ago(30d) .. ago(60d)) ,
ThirdMonth = Runtime_,TimeGenerated between(ago(60d) .. ago(90d) ) by Resource
有谁知道我在这里缺少或监督。这有可能与kusto?我是否可以使用查询开销来完成可以在几行中完成的任务。
如果我理解你的情况正确,你可以使用sumif
实现这一点,假设你事先知道你的目标月份。
这是一个例子:
datatable(Resource:string, Runtime:double, TimeGenerated:datetime)
[
"A", 13.4, datetime(2019-01-01 11:11:11),
"B", 1.34, datetime(2019-01-01 10:10:10),
"C", 0.13, datetime(2019-01-01 12:12:12),
"A", 12.4, datetime(2019-02-01 11:11:11),
"B", 1.24, datetime(2019-02-01 09:09:09),
"B", 2.24, datetime(2019-02-01 09:10:09),
"B", 3.24, datetime(2019-02-01 09:11:09),
"C", 0.12, datetime(2019-02-01 08:08:08),
"A", 14.4, datetime(2019-03-01 07:07:07),
"B", 1.44, datetime(2019-03-01 05:05:05),
"C", 0.14, datetime(2019-03-01 06:06:06),
]
| summarize Month1 = sumif(Runtime, TimeGenerated between(datetime(2019-01-01)..datetime(2019-02-01))),
Month2 = sumif(Runtime, TimeGenerated between(datetime(2019-02-01)..datetime(2019-03-01))),
Month3 = sumif(Runtime, TimeGenerated between(datetime(2019-03-01)..datetime(2019-04-01)))
by Resource