Search code examples
kdb

Compare two compound columns in two tables


I have two tables(see below) with sym and lp. I want to pull out every row from tab1 that doesn't have the complete set of symbols corresponding to the same sym from tab2.

tab1:([]sym:`EUR`AUD`GBP;lp:(`aa`bb`cc;`dd`ee;`ff`gg`aa`ee))
tab2:([]sym:`EUR`AUD`GBP;lp:(`aa`bb`ff`cc;`ee`dd;`gg`ff`ee`aa`rr`xx))

i.e. my result should be:

tab3:([]sym:`EUR`GBP;lp:(`ff;`rr`xx))

Thanks


Solution

  • I think this might fit what you're looking for:

    q)b: where 0 <> count each a: (exec lp from tab2) except' (exec lp from tab1)
    q)update lp: a b from tab1 b
    sym lp
    ----------
    EUR ,`ff
    GBP `rr`xx
    

    One assumption that I've made is that you always have the syms in the same order in both tables, is this always true?