【问题标题】:How can I filter out all elements except the newest ones?如何过滤掉除最新元素之外的所有元素?
【发布时间】:2021-11-12 17:31:37
【问题描述】:

我想做一些看似简单的事情,但正在努力确定如何在 KQL 中完成。假设您有一个包含 DateTime 列的结果集。我想要一个 Kusto 查询,它只保留具有最新 DateTime 的行。

所以对于这样的结果集:

Person Ingestion DateTime
Bob 11/12/2021 9 AM
Sam 11/12/2021 10 AM
William 11/12/2021 11 AM
Kate 11/12/2021 3 PM
Aria 11/12/2021 3 PM
Ben 11/9/2021 4 AM

我想要一个删除除 Kate 和 Aria 之外的所有行的查询,因为它们在结果集中都有最新的 DateTime。

我一直在尝试总结 max(),它似乎对 DateTimes 进行了分组,但没有过滤掉旧的。有没有办法先排序然后过滤?

【问题讨论】:

    标签: kql


    【解决方案1】:

    您可以有一个计算标量值等于最大日期时间值的子查询,然后使用该标量值进行过滤。

    例如:

    datatable(Person:string, IngestionDateTime:datetime)
    [
        'Bob', datetime(11/12/2021 09:00),
        'Sam', datetime(11/12/2021 10:00),
        'William', datetime(11/12/2021 11:00),
        'Kate', datetime(11/12/2021 15:00),
        'Aria', datetime(11/12/2021 15:00),
        'Ben', datetime(11/9/2021 04:00)
    ]
    | as T
    | where IngestionDateTime == toscalar(T | summarize max(IngestionDateTime))
    
    Person IngestionDateTime
    Kate 2021-11-12 15:00:00.0000000
    Aria 2021-11-12 15:00:00.0000000

    【讨论】:

      猜你喜欢
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 2023-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多