2013-06-23 63 views
1

我有表trade:([]time:`time$(); sym:`symbol$(); price:`float$(); size:`long$()) 與例如1000條記錄,例如10個獨特的syms。我想總結每個sym的前4個價格。如何從單個記錄中總結多個元素

我的代碼如下所示:

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

price| 14.71512 153.2244 154.1642 196.5744 170.6052 61.26522 45.70606 
46.9057.. 

當我想在res計算元素:count res

1 

我認爲水庫是有許多值的單個記錄,我怎麼能概括所有這些值,或者我怎麼可以總結第一對於?

+0

如果mnestor的答案解決了您的問題,您應該將其標記爲已接受。如果沒有,請告訴我們還缺什麼。 – mollmerx

回答

2

您可以使用「每個」對每一行運行總和:

select sum each price from res 

或者,如果你想在resoultTable運行:

select sum each price from resoultTable 

總結前四倍的價格爲每行,使用二進每個右:

select sum each 4#/:price from resoultTable 

或者你可以做到這一切很容易,一步到位:

select COUNT:count i, SUM:sum price, SUM4:sum 4#price by sym from trade