kdb +:对多列进行分组和求和

时间:2015-04-29 12:35:46

标签: kdb

考虑以下数据:

table:

time          colA    colB     colC
-----------------------------------
11:30:04.194  31      250      a
11:30:04.441  31      280      a
11:30:14.761  31.6    100      a
11:30:21.324  34      100      a
11:30:38.991  32      100      b
11:31:20.968  32      100      b
11:31:56.922  32.2    1000     b
11:31:57.035  32.6    5000     c
11:32:05.810  33      100      c
11:32:05.810  33      100      a
11:32:14.461  32      300      b

现在,只要sum相同,我colB colC怎么办,而不会丢失时间顺序。

所以输出结果为:

first time    avgA    sumB     colC
-----------------------------------
11:30:04.194  31.2    730      a
11:30:38.991  32.07   1200     b
11:31:57.035  32.8    5100     c
11:32:05.810  33      100      a
11:32:14.461  32      300      b

到目前为止我所拥有的:

select by time from (select first time, avg colA, sum colB by colC, time from table)

但输出未按colC分组。查询应该如何?

1 个答案:

答案 0 :(得分:1)

使用differ实现此目的的方法略有不同:

value select  first time, avg colA, sum colB , first colC by g:(sums differ colC) from table