使用 Kusto 获取每组的前 1 行

cmo*_*mah 8 group-by partition-by kql azure-data-explorer kusto-explorer

我有一个表,我想使用 Kusto 查询语言获取每个组的最新条目。这是表格:

文档状态日志

ID 文档ID 地位 创建日期
2 1 S1 2011年7月29日
3 1 S2 2011年7月30日
6 1 S1 2011年8月2日
1 2 S1 2011年7月28日
4 2 S2 2011年7月30日
5 2 S3 2011年8月1日
6 3 S1 2011年8月2日

该表将按 DocumentID 分组并按 DateCreated 降序排序。对于每个 DocumentID,我想获取最新状态。

我的首选输出:

文档ID 地位 创建日期
1 S1 2011年8月2日
2 S3 2011年8月1日
3 S1 2011年8月2日

有没有办法使用 KQL 只获取每个组中的顶部?

GetOnlyTheTop伪代码如下:

SELECT
  DocumentID,
  GetOnlyTheTop(Status),
  GetOnlyTheTop(DateCreated)
FROM DocumentStatusLogs
GROUP BY DocumentID
ORDER BY DateCreated DESC
Run Code Online (Sandbox Code Playgroud)

图片来源:改编自 DPP 的 SQL 问题:获取每组的前 1 行

Yon*_*i L 25

您可以使用partition运算符arg_max() 聚合函数

例如:

DocumentStatusLogs
| partition by DocumentId
(
    top 1 by DateCreated desc
) 
Run Code Online (Sandbox Code Playgroud)

或者

DocumentStatusLogs
| summarize arg_max(DateCreated, *) by DocumentId
Run Code Online (Sandbox Code Playgroud)