Search code examples
kdb

kdb/q functional update for "prev 5 mavg col1"


How do i write functional update for below? instead of spelling out each column (of 0, 1, 2, 3 ... ) because there are a lot of them.

table: update pma0: prev 5 mavg fa_0, pma1: prev 5 mavg fa_1, pma2: prev 5 mavg fa_2, pma3: prev 5 mavg fa_3, pma4: prev 5 mavg fa_4 by sym from table

It's particularly difficult for me because "mavg" is not really a "function" I call like like mavg[a;b] and there is another function "prev" ahead of it.

I can't even figure out an easier version of the problem. if I just want to

table: update pma0: prev fa_0, pma1: prev fa_1, pma2: prev fa_2, pma3: prev fa_3, pma4: prev fa_4 by sym from table

below apparently didn't work. It gives me the same values for newCols and origCols.

ttt2: ![table; (); enlist[`ric]!enlist[`ric]; newCols!({prev; x} each origCols)]

Solution

  • You may use functional form similar to following

    table: ![table;();(1#`sym)!1#`sym;(`$"pma",/:string til 5)!{(prev;(mavg;5;x))} each `$"fa_",/:string til 5];
    

    Where (`$"pma",/:string til 5) creates list column names. And {(prev;(mavg;5;x))} each `$"fa_",/:string til 5 creates list of corresponding column values in functional form