Saturday, 15 February 2014

q lang - Functional addition of Columns in kdb+q -



q lang - Functional addition of Columns in kdb+q -

i have q table in no. of non keyed columns variable. also, these column names contain integer in names. want perform function on these columns without using actual names

how can accomplish ?

for example:

table: | col10 col20 col30 1 | 2 3 4 2 | 5 7 8 // assume have numbers 10, 20 ,30 obtained column names want **update newcol:10*col10+20*col20+30*col30 table** except no.of columns not fixed inlcluded numbers

we want utilize functional update (simple illustration shown here: http://www.timestored.com/kdb-guides/functional-queries-dynamic-sql#functional-update)

for particular query want generate computation tree of select clause, i.e. lastly part of functional update statement. easiest way parse similar statement recreate format:

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) t" ! `t () 0b (,`r)!,(+;(*;10;`c10);(+;(*;20;`col20);(*;30;`col30))) q)/ notice lastly value, parse tree q)/ want recreate using code q){(*;x;`$"c",string x)} 10 * 10 `c10 q){(+;x;y)} on {(*;x;`$"c",string x)} each 10 20 + (*;10;`c10) (*;20;`c20) q)maketree:{{(+;x;y)} on {(*;x;`$"c",string x)} each x} / write 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) t c10 c20 c30 c40 r ------------------- 1 10 7 0.4 420 2 20 8 0.5 660 3 30 9 0.6 900

kdb q-lang

No comments:

Post a Comment