Search code examples
kdb

KDB+ Merging multiple update statements


q)d:([] f1:`a`b` ;f2:```c; m1:`x``z;m2:``y`z)

f1 f2 m1 m2
-----------
a     x    
b        y 
   c  z  z 

I want to update the f1 & m1 columns to f2 & m2 respectively if f1 & m1 have nulls; actually I want to merge these 2 queries to one update statement :

update f1:f2 from d where null f1
update m1:m2 from d where null m1`

Solution

  • You can use Triadic vector conditional evaluation ?

    ?[vb;exprtrue;exprfalse]
    

    The new query would be :

    q)update f1:?[null f1;f2;f1] , m1:?[null m1;m2;m1] from d
    f1 f2 m1 m2
    -----------
    a     x    
    b     y  y 
    c  c  z  z