kdb+:对多列进行分组和求和
kdb+: group by and sum over multiple columns
考虑以下数据:
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
分组。查询应该是什么样的?
这个怎么样?
get select first time, avg colA, sum colB, first colC by sums colC<>prev colC from table
使用 differ
实现此目的的一种稍微不同的方法:
value select first time, avg colA, sum colB , first colC by g:(sums differ colC) from table
考虑以下数据:
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
分组。查询应该是什么样的?
这个怎么样?
get select first time, avg colA, sum colB, first colC by sums colC<>prev colC from table
使用 differ
实现此目的的一种稍微不同的方法:
value select first time, avg colA, sum colB , first colC by g:(sums differ colC) from table