使用 KQL(Kusto 查询语言),如何将日期时间分组为周(或 7 天的块)?

发布于 2025-01-10 13:52:30 字数 727 浏览 0 评论 0原文

我正在针对 Azure Application Insights 运行 KQL(Kusto 查询语言)查询。我想每周汇总某些测量结果。我想弄清楚如何将我的数据分成几周。

为了说明我所寻求的内容,这里有一个计算duration列的每日平均值的查询。

requests 
| where timestamp > ago(7d)
| summarize 
    avg(duration)
    by 
        Date = format_datetime(timestamp, "yyyy-MM-dd")

这会产生类似的内容:

在此处输入图像描述

在上面,我已将日期时间转换为字符串,从而有效地将它们“四舍五入”到一天的精度。这可能很难看,但这是我能想到的对某一天的所有结果进行分组的最简单的方法。使用相同的技术将四舍五入到几个月或几年是微不足道的。

但是如果我想按周对日期时间进行分组怎么办?有什么好的方法可以做到这一点吗?

我不在乎我的“周”是从周一、周日、1 月 1 日还是其他什么开始。我只想将 KQL 日期时间集合分组为 7 天的块。我怎样才能做到这一点?

提前致谢!

I am running KQL (Kusto query language) queries against Azure Application Insights. I have certain measurements that I want to aggregate weekly. I am trying to figure out how to split my data into weeks.

To illustrate what I seek, here is a query that computes daily averages of the duration column.

requests 
| where timestamp > ago(7d)
| summarize 
    avg(duration)
    by 
        Date = format_datetime(timestamp, "yyyy-MM-dd")

This produces something similar to this:

enter image description here

In the above I have converted datetimes to string and thus effectively "rounded them down" to the precision of one day. This may be ugly, but it's the easiest way I could think of in order to group all results from a given day. It would be trivial to round down to months or years with the same technique.

But what if I want to group datetimes by week? Is there a nice way to do that?

I do not care whether my "weeks" start on Monday or Sunday or January 1st or whatever. I just want to group a collection of KQL datetimes into 7-day chunks. How can I do that?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开柳相依 2025-01-17 13:52:30

看起来您正在寻找“bin()”函数:

requests 
| where timestamp > ago(7d)
| summarize 
    avg(duration)
    by 
        bin(timestamp, 1d) // one day, for 7 days change it to 7d

Looks like you are looking for the "bin()" function:

requests 
| where timestamp > ago(7d)
| summarize 
    avg(duration)
    by 
        bin(timestamp, 1d) // one day, for 7 days change it to 7d
望笑 2025-01-17 13:52:30

我发现我可以使用 week_of_year 函数按周数分割日期时间:

requests 
| where timestamp > ago(30d)
| summarize 
    avg(duration)
    by 
        Week = week_of_year(timestamp)
| sort by Week

在此处输入图像描述

I found out that I can use the week_of_year function to split datetimes by week number:

requests 
| where timestamp > ago(30d)
| summarize 
    avg(duration)
    by 
        Week = week_of_year(timestamp)
| sort by Week

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文