Search code examples
kdb

Utility like except for tables in kdb


As we have except function for lists in kdb to find the elements which are present in one list and not in another, similarly do we have any utility to extract the rows present in one table and not in another based on a column?

Eg: I have two tables:

l:([]c1:`a`b`c`d;c2:10 20 30 40)
r:([]c1:`a`a`a`b`b;c3:100 200 300 400 50)

Since, for column c1 in table l we have row c d which are not present in column c1 of table r.
Do we have any utility in kdb which can be used to get output like below?

c1 c2
-----
c  30
d  40

I got the output using -

select from l where c1 in l[`c1] except r`c1

But, I'm searching for better/optimised solution/utility to get the same output.


Solution

  • I don't think there's anything wrong with your current implementation but you could use drop (aka _) on a keyed table for a more succinct approach:

    q)#[1#`c1;r]_1!l
    c1| c2
    --| --
    c | 30
    d | 40
    

    This also remains pretty neat when they "key" is more than one column:

    l0:([]c0:`x`y`z`w;c1:`a`b`c`d;c2:10 20 30 40)
    r0:([]c0:`y`x`x`x`y;c1:`a`a`a`b`b;c3:100 200 300 400 50)
    
    q)#[`c0`c1;r0]_2!l0
    c0 c1| c2
    -----| --
    z  c | 30
    w  d | 40