【问题标题】:Get Other columns based on max of one column in Kusto根据 Kusto 中最多一列获取其他列
【发布时间】:2021-08-24 15:10:38
【问题描述】:

我正在尝试编写 Kusto 查询以查找在由另一列分组的列中具有最大值但还需要第三(剩余)列的记录。

假设有三列 A(timestamp) B(impvalue: number) 和 C(anothervalue:string)。 我还需要获取按 C 分组的记录以及最大时间戳及其对应的 B 列。

在 Sql 中,我很清楚如何使用自连接。我是 Kusto 的新手,我尝试了一些与 summarise、join 和 top 运算符的组合,但未能成功。

例子:

输出:

【问题讨论】:

    标签: azure-data-explorer kql appinsights kusto-explorer


    【解决方案1】:

    您可以使用arg_max()聚合函数:https://docs.microsoft.com/en-us/azure/data-explorer/kusto/query/arg-max-aggfunction

    例如:

    datatable(A:datetime, B:long, C:string)
    [
        datetime(2020-08-20 12:00:00), 50,  "abc",
        datetime(2020-08-20 12:10:00), 30,  "abc",
        datetime(2020-08-20 12:05:00), 100, "abc",
        datetime(2020-08-20 12:00:00), 40,  "def",
        datetime(2020-08-20 12:05:00), 120, "def",
        datetime(2020-08-20 12:10:00), 80,  "def",
    ]
    | summarize arg_max(A, *) by C
    
    C A B
    abc 2020-08-20 12:10:00.0000000 30
    def 2020-08-20 12:10:00.0000000 80

    【讨论】:

      【解决方案2】:

      这不是最优雅的解决方案,但它确实有效:

      let X = datatable (a: string, b: int, c: string) [
      "8/24/2021, 12:40:00.042 PM", 50, "abc",
      "8/24/2021, 12:40:10.042 PM", 30, "abc",
      "8/24/2021, 12:40:05.042 PM", 100, "abc",
      "8/24/2021, 12:40:00.042 PM", 40, "def",
      "8/24/2021, 12:40:05.042 PM", 120, "def",
      "8/24/2021, 12:40:10.042 PM", 80, "def"
      ];
      X
      | summarize Answer = max(a)
      | join X on $left.Answer == $right.a
      | project a,b,c
      

      【讨论】:

      • 这个解决方案效率很低(因为不需要join)。相反,应该使用summarize arg_max(...),就像 Yoni 回答的那样。我建议删除这个答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 2022-11-11
      • 2021-11-25
      • 2014-04-23
      • 2019-05-29
      相关资源
      最近更新 更多