kdb如何计算滚动计数
kdb how to calculate rolling count
假设我有 table 个事件,带有时间戳和类型。
t1, 'b'
t2, 'x'
t3, 's'
t4, 'b'
我怎样才能得到一个滚动计数,这样它会给我一个所有时间戳的列表和最多 taht ts 的事件的累积数量,有点像求和的计数版本
for example for 'b' I d like a table
't1', 1
't2', 1
't3', 1
't4', 2
这是一种方法,但可能还有更聪明的方法,即使用求和:
//table definition
tab:([]a:`t1`t2`t3`t4;b:"bxsb")
//rolling sum of 1 by column b
update sums count[i]#1 by b from tab
结果:
a b x
------
t1 b 1
t2 x 1
t3 s 1
t4 b 2
如果你想替换 b 你只需将 b:
放在 sums
前面。
一种方式:
q)t:([]p:asc 4?.z.p+til 1000;t:`b`x`s`b)
q)asc `p xcols ungroup select p,til count i by t from t
p t x
---------------------------------
2017.05.16D09:42:48.259062090 b 0
2017.05.16D09:42:48.259062585 x 0
2017.05.16D09:42:48.259062683 s 0
2017.05.16D09:42:48.259062858 b 1
Ps:请注意,我已经从 0 开始序列,就好像在说 "I've had 0 events prior to this row" 而不是按照您的示例从 1 开始。它符合您的要求 "number of events up to that ts"。如果您需要 1,只需添加 1 '1+til count i'。还要确保您的时间已排序,以便在开始序列时有意义。
与tablet
如下:
q)show t: ([]ts:.z.t - desc "u"$(til 4);symb:`b`x`z`b)
ts symb
-----------------
09:46:56.384 b
09:47:56.384 x
09:48:56.384 z
09:49:56.384 b
使用条件向量:
q)select ts, cum_count:sums ?[symb=`b;1;0] from t
ts cum_count
----------------------
09:46:56.384 1
09:47:56.384 1
09:48:56.384 1
09:49:56.384 2
相同,但有一个以symb
为参数的函数:
q){select ts, cum_count:sums ?[symb=x;1;0] from t}[`b]
ts cum_count
----------------------
09:46:56.384 1
09:47:56.384 1
09:48:56.384 1
09:49:56.384 2
事实上,您不需要条件向量,因为您可以直接对布尔值求和:
q){select ts, cum_count:sums symb=x from t}[`b]
ts cum_count
----------------------
09:46:56.384 1
09:47:56.384 1
09:48:56.384 1
09:49:56.384 2
假设我有 table 个事件,带有时间戳和类型。
t1, 'b'
t2, 'x'
t3, 's'
t4, 'b'
我怎样才能得到一个滚动计数,这样它会给我一个所有时间戳的列表和最多 taht ts 的事件的累积数量,有点像求和的计数版本
for example for 'b' I d like a table
't1', 1
't2', 1
't3', 1
't4', 2
这是一种方法,但可能还有更聪明的方法,即使用求和:
//table definition
tab:([]a:`t1`t2`t3`t4;b:"bxsb")
//rolling sum of 1 by column b
update sums count[i]#1 by b from tab
结果:
a b x
------
t1 b 1
t2 x 1
t3 s 1
t4 b 2
如果你想替换 b 你只需将 b:
放在 sums
前面。
一种方式:
q)t:([]p:asc 4?.z.p+til 1000;t:`b`x`s`b)
q)asc `p xcols ungroup select p,til count i by t from t
p t x
---------------------------------
2017.05.16D09:42:48.259062090 b 0
2017.05.16D09:42:48.259062585 x 0
2017.05.16D09:42:48.259062683 s 0
2017.05.16D09:42:48.259062858 b 1
Ps:请注意,我已经从 0 开始序列,就好像在说 "I've had 0 events prior to this row" 而不是按照您的示例从 1 开始。它符合您的要求 "number of events up to that ts"。如果您需要 1,只需添加 1 '1+til count i'。还要确保您的时间已排序,以便在开始序列时有意义。
与tablet
如下:
q)show t: ([]ts:.z.t - desc "u"$(til 4);symb:`b`x`z`b)
ts symb
-----------------
09:46:56.384 b
09:47:56.384 x
09:48:56.384 z
09:49:56.384 b
使用条件向量:
q)select ts, cum_count:sums ?[symb=`b;1;0] from t
ts cum_count
----------------------
09:46:56.384 1
09:47:56.384 1
09:48:56.384 1
09:49:56.384 2
相同,但有一个以symb
为参数的函数:
q){select ts, cum_count:sums ?[symb=x;1;0] from t}[`b]
ts cum_count
----------------------
09:46:56.384 1
09:47:56.384 1
09:48:56.384 1
09:49:56.384 2
事实上,您不需要条件向量,因为您可以直接对布尔值求和:
q){select ts, cum_count:sums symb=x from t}[`b]
ts cum_count
----------------------
09:46:56.384 1
09:47:56.384 1
09:48:56.384 1
09:49:56.384 2