在选择查询中,根据 q kdb 中的 if -else if 条件设置列值

单位

我们有一个表 t:

t:([] sym:`GOOG`IBM`APPL; px:10 20 30; size:1000 2000 3000)

现在我们要根据函数中提供的条件选择在输出中分配一列。

{[m]select sym, px, size, eb:?[`ab=m;`cd;`ef] from t where size>1000}[`ab] / This works fine providing proper value to eb in output(if/else)

但我的要求是基于 (if/else if) 将 eb 的值设置为如下,尝试过 ?, $ 但没有用

{[m]select sym, px, size, eb:?[`ab=m;`cd;`yz=m;`ef] from t where size>1000}[`ab] / It fails with type error 

要求(须藤代码):

if (m==ab) { return cd};
else if (m==yz) {return ef};
托马斯·史密斯

使用向量条件时,?您需要嵌套条件。在此示例中,不匹配任一条件将返回空值。

q){[m]select sym, px, size, eb:?[`ab=m;`cd;?[`yz=m;`ef;`]] from t where size>1000}[`ab]
sym  px size eb
---------------
IBM  20 2000 cd
APPL 30 3000 cd

如果您有很多离散条件,这可能会变得非常笨拙,另一种方法是使用字典。

q)dict:`ab`yz!`cd`ef
q){[m]select sym, px, size, eb:dict[m] from t where size>1000}[`ab]
sym  px size eb
---------------
IBM  20 2000 cd
APPL 30 3000 cd

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章