在kdb + q中添加列的功能

pik*_*eon 1 kdb q-lang

我有aq表,其中没有.非键控列是可变的.此外,这些列名称在其名称中包含一个整数.我想在这些列上执行某些功能而不实际使用它们的实际名称

我怎样才能做到这一点?

例如:

 table:   

 a |  col10  col20 col30 

 1 |    2      3     4
 2 |    5      7     8

 // Assume that I have numbers 10, 20 ,30 obtained from column names

    I want something like **update NewCol:10*col10+20*col20+30*col30 from table**

     except that no.of columns is not fixed so are their inlcluded numbers
Run Code Online (Sandbox Code Playgroud)

Rya*_*ton 6

我们想要使用功能更新(这里显示的简单示例:http://www.timestored.com/kdb-guides/functional-queries-dynamic-sql#functional-update)

对于此特定查询,我们希望生成select子句的计算树,即功能更新语句的最后部分.最简单的方法是解析类似的语句,然后重新创建该格式:

q)/ create our table
q)t:([] c10:1 2 3; c20:10 20 30; c30:7 8 9; c40:0.1*4 5 6)
q)t
c10 c20 c30 c40
---------------
1   10  7   0.4
2   20  8   0.5
3   30  9   0.6

q)parse "update r:(10*c10)+(20*col20)+(30*col30) from t"
!
`t
()
0b
(,`r)!,(+;(*;10;`c10);(+;(*;20;`col20);(*;30;`col30)))
q)/ notice the last value, the parse tree
q)/ we want to recreate that using code
q){(*;x;`$"c",string x)} 10
*
10
`c10
q){(+;x;y)} over {(*;x;`$"c",string x)} each 10 20
+
(*;10;`c10)
(*;20;`c20)
q)makeTree:{{(+;x;y)} over {(*;x;`$"c",string x)} each x}

/ now write as functional update
q)![t;();0b; enlist[`res]!enlist makeTree 10 20 30]
c10 c20 c30 c40 res
-------------------
1   10  7   0.4 420
2   20  8   0.5 660
3   30  9   0.6 900

q)update r:(10*c10)+(20*c20)+(30*c30) from t
c10 c20 c30 c40 r
-------------------
1   10  7   0.4 420
2   20  8   0.5 660
3   30  9   0.6 900
Run Code Online (Sandbox Code Playgroud)