如何将数组传递给更新查询kdb

时间:2018-09-09 10:48:03

标签: kdb

你好,我有以下代码,我想找到传递任何大小的数组的最佳方法,而不必一行一行地显式地进行操作:

两个数组是系数和行情指示器。谢谢

t:update Value:Value*coeffs[0] from (select from t)where Code in `$tickers[0];
t:update Value:Value*coeffs[1] from (select from t)where Code in `$tickers[1];
t:update Value:Value*coeffs[2] from (select from t)where Code in `$tickers[2];
t:update Value:Value*coeffs[3] from (select from t)where Code in `$tickers[3];
t:update Value:Value*coeffs[4] from (select from t)where Code in `$tickers[4];

2 个答案:

答案 0 :(得分:2)

假设两个数组的长度相同,则可以尝试创建tickerscoeffs的字典:

dict:(`$tickers)!coeffs

然后可以在update语句中使用它:

update Value:Value*1^dict[Code] from t

1^在这里至关重要,因为使用不存在的键索引到dict时将返回null。这种表示法允许您使用1fill空值,从而确保Value保持不变。

答案 1 :(得分:1)

完成该任务的另一种方法是使用lj

q)t:([] n:til 10; Value:1+til 10; Code:10#`a`b`c`d`e)
q)tickers:enlist each "abcdf"

使用keyedkt创建tickerscoeffs

q)kt:([Code:`$tickers] coeffs:2 4 6 8 10 )
q)kt
Code| coeffs
----| ------
a   | 2
b   | 4
c   | 6
d   | 8
f   | 10

现在将tkt一起加入

q)t:t lj kt
q)t
n Value Code coeffs
-------------------
0 1     a    2
1 2     b    4
2 3     c    6
3 4     d    8
4 5     e
5 6     a    2
6 7     b    4
7 8     c    6
8 9     d    8
9 10    e

更新具有t non-null个值的表coeff

q)update Value:Value*coeffs from t where not null coeffs
n Value Code coeffs
-------------------
0 2     a    2
1 8     b    4
2 18    c    6
3 32    d    8
4 5     e
5 12    a    2
6 28    b    4
7 48    c    6
8 72    d    8
9 10    e
  • 使用lj,您最终会有一个额外的列coeffs,您可能想删除。
  • 当您有多个映射(tickers->coeffstickers->delta等)时,只需要创建一个包含所有映射的表,这特别有用。