Apply formula to current and previous rows only (Q/KDB)

↘锁芯ラ 提交于 2019-12-05 15:55:28

Not entirely sure what you want but the following uses the latest price for each sym to calculate the sum rp:

q)update rp:sum each @\[()!();sym;:;price] from data
dt                            sym  price shares rp
-----------------------------------------------------
2017.01.05D19:45:00.238248239 AAPL 101.2 500    101.2
2017.01.05D20:46:00.282382392 GOOG 800.2 100    901.4
2017.01.05D21:47:00.232842342 AAPL 102.3 500    902.5
2017.01.05D22:48:00.835838442 BBRY 2.2   900    904.7
2017.01.05D20:49:00.282382392 GOOG 800.5 100    905

Which gives the same answer for the final data point as you have given above.

You can also get the last price at each index, like so:

{[x;y] exec sum price from x where i<=y, i=(last;i) fby sym}[data]each til count data
101.2 901.4 902.5 904.7 905
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!