Search code examples
kdb

How to add p# to a table using functional amend?


I have a query that returns data for a bunch of syms for a date from a table.

dataFromTab:delete date from select from table where date=2015.01.01,any (sym like) each ("sym1";"sym2";"sym3";"sym4")
dataFromTab:`sym xasc dataFromTab;
@[`dataFromTab;`sym;`p#];

type var is 98h and sorting by sym is working fine but when adding p# using functional amend is giving me a 'type error.

Any inputs on where i am going wrong would be appreciated.

Thanks!


Solution

  • Are you calling all of this from within a function or from the global namespace?

    If dataFromTab is a local variable then applying the p# to the global referencedataFromTab will not work.

    q){data:select from tab;@[`data;`sym;`p#]}[]
    'type
     [1]  {data:select from tab;@[`data;`sym;`p#]}
                             ^
    q))\
    

    You will need to reassign dataFromTab locally for this to work;

    q){data:select from tab;data:@[data;`sym;`p#]}[]
    

    You could also use 'set' to create your table globally, which would allow your original syntax to work. However you probably do not want to create unnecessary globals;

    q){`data set select from tab;@[`data;`sym;`p#]}[]
    `data