kdb

filtering columns that have lists for rows based on other scalar columns in kdb


w:(1 2;3 4;5 2); l:til count w; h:l+1;
show t:([] l; h; w);

l h w  
-------
0 1 1 2
1 2 3 4
2 3 5 2

I have a table t with a column w which has lists for rows. I would like to only keep elements in each row of w which lie between the l and h values for each row of the table.

The expected output is:

l h w  
-------
0 1 ,1
1 2 ,
2 3 ,2

I tried doing

t:update w:w[where ((w>=l) and (w<=h))] from t;

but it fails with

'type
  [0]  t:update w:w[where ((w>=l) and (w<=h))] from t;


Solution

  • You can use within (https://code.kx.com/q/ref/within)

    q)update w:w@'where each w within(l;h)from t
    l h w
    ------------
    0 1 ,1
    1 2 `long$()
    2 3 ,2
    

    Your original attempt was close, here's the corrected version:

    q)update w:w@'where each(w>=l)and w<=h from t
    l h w
    ------------
    0 1 ,1
    1 2 `long$()
    2 3 ,2
    

    You needed to consider the table columns as lists (so you need the each and each-both (') iterators.