如何基于映射表在KDB+中更新列以获取实际字段值?
KDB+函数式更新中根据映射获取列实际值的问题
现有KDB+表:
tab:([]exec_location_1:`PA1`DNS`DNS;quantity_s9:8 40 20;mf_quantity_s17:123 123 4;eq_quantity_s17:10 20 30)
需要通过以下映射规则为表新增quantity列:根据exec_location_1的值匹配到对应的列名,再取该列的实际数值:
orderQuantityMap:`NEW`XRM`PA1`PA3`PA4`PA5`XRT`XRB`MF1`DNS!(`quantity_s9`quantity_s9`quantity_s9`quantity_s9`quantity_s9`quantity_s9`quantity_s9`quantity_s9`mf_quantity_s17`eq_quantity_s17);
使用原函数式更新语句时,仅返回了列名而非实际数值:
![tab;();0b;(enlist `quantity)!enlist(orderQuantityMap;`exec_location_1)]
问题原因
原语句中(orderQuantityMap;exec_location_1)`仅完成了从映射表中匹配列名的操作,返回的是对应列的符号名,并没有从原表中提取该列的实际数值,因此结果只显示列名而非数据。
解决方法
方法1:使用点运算符(.)提取对应值
点运算符可以根据每行的列名,直接从表中取出对应位置的数值:
![tab;();0b;(enlist `quantity)!enlist tab . (orderQuantityMap;`exec_location_1)]
执行后得到目标表:
exec_location_1 quantity_s9 mf_quantity_s17 eq_quantity_s17 quantity -------------------------------------------------------------------- PA1 8 123 10 8 DNS 40 123 20 20 DNS 20 4 30 30
方法2:使用get each逐个获取列值
通过get函数根据列名提取对应列内容,结合each遍历每行的匹配列名:
![tab;();0b;(enlist `quantity)!enlist (get each; (orderQuantityMap;`exec_location_1))]
对应的命令式更新写法为:
update quantity: get each orderQuantityMap[exec_location_1] from tab
内容的提问来源于stack exchange,提问作者kka
相关产品推荐
相关产品推荐

