Search code examples
kdb

KDB how to use $[] in query


If I have a table

t: flip `a`b`c!(til 10;10?(1b,0b);10?(1b,0b))

why can I do this

update x:?[b;1;?[c;3;0]] from t

but not

update x:$[b;1;c;3;0] from t

[which returns a rank error] while this

b:0b
c:1b
$[b;1;c;3;0]

works fine?


Solution

  • $ is the standard conditional operator that only works with atomic values. ? is vector conditional operator that works with both atomic and vector conditions.

    The columns in a table are vectors and as a result using $ results in an error whereas ? does not. To use $ with a table you need to pass the values to it individually, an example of this might be:

    update x:{$[x;1;y;3;0]}'[b;c] from t