Kusto查询以获取给定日期的累计计数

时间:2019-09-30 19:10:36

标签: kusto azure-data-explorer kusto-query-language

我有一个数据库,其中包含一组具有用户ID和时间戳的事件,并且我正在尝试编写一个查询,该查询将提供给我每天触发事件的不同用户的数量。因此,如果我们有以下数据:

Event   | UID | Time Stamp
event 1 |  0  | 9/25/19 9:00 AM 
event 2 |  1  | 9/25/19 3:00 PM 
event 3 |  2  | 9/26/19 2:00 PM 
event 4 |  1  | 9/28/19 5:00 PM 
event 5 |  3  | 9/29/19 7:00 AM 

然后输出应为:

9/25/19 : 2
9/26/19 : 3
9/27/19 : 3 (since there are no new events on the 27th)
9/28/19 : 3 (since user with UID=1 has already been counted)
9/29/19 : 4

我有一个查询,该查询将获取每天的事件数,而不是直到该天为止的所有天的事件数。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

在Kusto / ADX中有几个内置的用户分析插件:https://docs.microsoft.com/en-us/azure/kusto/query/useranalytics

例如,其中一个是LocalDate插件:https://docs.microsoft.com/en-us/azure/kusto/query/activity-engagement-plugin

例如:

activity_engagement

,如果您想“填补” 9月27日的空白,可以执行以下操作:

let T = datatable(Event:string, UID:int, Timestamp:datetime)
[
    'event 1', 0,  datetime(9/25/19 9:00 AM),
    'event 2', 1,  datetime(9/25/19 3:00 PM), 
    'event 3', 2,  datetime(9/26/19 2:00 PM),
    'event 4', 1,  datetime(9/28/19 5:00 PM),
    'event 5', 3,  datetime(9/29/19 7:00 AM),
]
;
let min_date_time = toscalar(T | summarize startofday(min(Timestamp)));
let max_date_time = toscalar(T | summarize startofday(max(Timestamp)));
T
| evaluate activity_engagement (UID, Timestamp, 1d, 1d + max_date_time - min_date_time)
| project Timestamp, dcount_activities_outer