sxzhangzsx
sxzhangzsx

Reputation: 75

kdb/q update multiple different values in one column for different key in keyed table

I have a table, which is :

t:([alpha:`a`b`c`d`e`f];beta:10 20 30 40 50 60)

I also have two variable:

chgkey:`b`c
chgvalue:2000 -1000

I want to do something like:

t:update beta:2000 from t where alpha=`b;
t:update beta:-1000 from t where alpha=`c;

My question is how to combine these two lines of code. Because I am working on a table which is far bigger than this simple example and it will take me a lot of rows of codes to do all the updates, which is too long.

It would be nice something like

t[chgkey]`beta:chgvalue;

Upvotes: 2

Views: 1434

Answers (2)

Alexander Belopolsky
Alexander Belopolsky

Reputation: 2268

It would be nice something like t[chgkey]`beta:chgvalue

A working solution is only slightly more complicated:

t[([]alpha:chgkey);`beta]:chgvalue

Upvotes: 3

Anton Dovzhenko
Anton Dovzhenko

Reputation: 2569

I would try to use the next query. But note that it may be slow in case if your dict is really huge

dict: `b`c ! 1000 2000;
t: update beta: ?[alpha in key dict; dict alpha;beta] from t;

In case if table t is global it can be updated with functional update:

{![`t;enlist (=;`alpha;enlist x);0b; enlist[`beta]!enlist y];}'[`b`c; 1000 2000]

Upvotes: 2

Related Questions