You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Kusto查询语言:合并两个行值计数或重命名行值的实现方法

这问题我熟!在Azure Log Analytics的Kusto查询里,咱们可以用case()函数轻松搞定这种值的合并或重命名,刚好对应你想要的两种需求,我给你拆解一下:

方案1:把PinkLadyApple和JazzApple统一重命名为Apple

如果想把这两个苹果品种合并成一个通用的「Apple」类别,你可以在聚合前先通过extend添加一个标准化后的字段,再基于这个新字段统计:

fruits
| where timestamp > ago(90d)
| where fruitname in ('PinkLadyApple', 'JazzApple', 'Orange', 'Banana')  // 记得把JazzApple也加入筛选条件哦
| extend standardizedFruit = case(
    fruitname in ('PinkLadyApple', 'JazzApple'), 'Apple',
    fruitname == 'Orange', 'Orange',
    fruitname == 'Banana', 'Banana',
    fruitname  // 兜底逻辑,防止有未覆盖的其他值
)
| summarize FruitCount = sum(OtherColumn) by standardizedFruit
| project Name = standardizedFruit, Sum = FruitCount

执行后你会得到合并后的统计结果:

Name | Sum
Apple | 120
Orange | 150
Banana | 80

方案2:把PinkLadyApple合并到JazzApple下

如果只想把旧品种的统计值合并到新品种「JazzApple」下,只需要调整case()的映射规则即可:

fruits
| where timestamp > ago(90d)
| where fruitname in ('PinkLadyApple', 'JazzApple', 'Orange', 'Banana')
| extend mergedFruit = case(
    fruitname == 'PinkLadyApple', 'JazzApple',
    fruitname  // 其他所有值保持原有名称
)
| summarize FruitCount = sum(OtherColumn) by mergedFruit
| project Name = mergedFruit, Sum = FruitCount

这样统计出来的结果就会把两个苹果的数值合并到JazzApple下:

Name | Sum
JazzApple | 120
Orange | 150
Banana | 80

小提示

如果后续需要合并更多水果品种,可以把同组的名称放进动态数组里,用in判断会更灵活,比如:

case(fruitname in dynamic(['PinkLadyApple', 'JazzApple', 'GalaApple']), 'Apple', fruitname)

内容的提问来源于stack exchange,提问作者Sam Polgar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 04:37:50