滚动时间窗口上的不同计数 [英] Distinct count on a rolling time window
问题描述
我想计算最近X分钟内出现的与众不同目录编号。通常将其称为滚动时间窗口。
I want to count the number of distinct catalog numbers that have appeared within the last X minutes. This is usually called a rolling time window.
例如,如果我有:
row startime orderNumber catalogNumb
1 2007-09-24-15.50 o1 21
2 2007-09-24-15.51 o2 21
3 2007-09-24-15.52 o2 21
4 2007-09-24-15.53 o3 21
5 2007-09-24-15.54 o4 22
6 2007-09-24-15.55 o4 23
7 2007-09-24-15.56 o4 21
8 2007-09-24-15.57 o4 21
例如,如果我想在最近5分钟内获得此值(5只是可能的值之一),则输出应为:
For instance, if I want to get this for the last 5 minutes (5 is just one of the possible values), the output should be:
row startime orderNumber catalogNumb countCatalog
1 2007-09-24-15.50 o1 21 1
2 2007-09-24-15.51 o2 22 2
3 2007-09-24-15.52 o2 23 3
4 2007-09-24-15.53 o3 24 4
5 2007-09-24-15.54 o4 21 4
6 2007-09-24-15.55 o4 21 4
7 2007-09-24-15.56 o4 21 4
8 2007-09-24-15.57 o4 21 3
我正在将Big SQL用于InfoSphere BigInsights v3.0。
结果查询可以使用任何db2 Olap Windows函数,但
计数(distinct catalogNumb)为OVER()...,但我的db2版本不支持。
I am using Big SQL for infosphere BigInsights v3.0. Resulting query can use any db2 Olap windows functions except for count (distinct catalogNumb) OVER()... which is not supported by my db2 version.
除了计数外,我可能还需要在catalogNumb和其他属性上使用其他聚合函数(avg,sum ...)。
In addition to count, I may also need to use other aggregate functions (avg, sum...) over the catalogNumb and other attributes.
任何反馈将不胜感激。
推荐答案
您可以尝试一些像这样:
You can try something like this:
select ...
from mytable
where starttime between current_time - 5 minutes and current_time
这将获取最近5分钟的所有行。 5
可以是一个变量。然后对行进行count()或sum()或average()。
That will get all the rows for the last 5 minutes. 5
can be a variable. then count() or sum() or average() the rows.
这篇关于滚动时间窗口上的不同计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!