如何从单个记录中求和多个元素 [英] How to sum multiple elements from single record
问题描述
我有桌子trade:([]time:`time$(); sym:`symbol$(); price:`float$(); size:`long$())
与1000条记录,例如10个独特的符号.我想对每个符号的前4个价格求和.
I have table trade:([]time:`time$(); sym:`symbol$(); price:`float$(); size:`long$())
with e.g. 1000 records, with e.g. 10 unique syms. I want to sum the first 4 prices for each sym.
我的代码如下:
priceTable: select price by sym from trade;
amountTable: select count price by sym from trade;
amountTable: `sym`amount xcol amountTable;
resultTable: amountTable ij priceTable;
所以我的新表如下:resultTable
sym | amount price
-------| --------------------------------------------------------------
instr0 | 106 179.2208 153.7646 155.2658 143.8163 107.9041 195.521 ..
命令结果:res: select sum price from resultTable where i = 1
:
price
..
----------------------------------
14.71512 153.2244 154.1642 196.5744
现在,当我想对元素求和时,会收到:sum res
Now, when I want to sum elements I receive: sum res
price| 14.71512 153.2244 154.1642 196.5744 170.6052 61.26522 45.70606
46.9057..
当我要计算res中的元素时:count res
When I want to count elements in res: count res
1
我假设res是具有多个值的单个记录,如何对所有这些值求和,或者如何首先求和?
I assume that res is a single record with many values, how can I sum all of those values, or how can I sum first for?
推荐答案
您可以使用"each"在每一行上求和:
You can use "each" to run the sum on each row:
select sum each price from res
或者如果您想在resoultTable上运行:
Or if you want to run on resoultTable:
select sum each price from resoultTable
要对每行的前四个价格求和,请使用二进位的each-right:
To sum the first four prices for each row, use a dyadic each-right:
select sum each 4#/:price from resoultTable
或者您可以一步很简单地完成所有这些操作:
Or you could do all of this very easily, in one step:
select COUNT:count i, SUM:sum price, SUM4:sum 4#price by sym from trade
这篇关于如何从单个记录中求和多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!