wwarby
wwarby

Reputation: 2051

Update only matched records in keyed KDB+ table with Q

I have a keyed table (tableA), and I want to update it with changes from another keyed (tableB) table, but only with records where the key in tableB matches a key in tableA. In other words, I do not want any new records in tableA, I only want to update the ones that area already there. tableA will have some columns that don't exist in tableB, and I want to preserve the original values of those columns. For example, if tableA and tableB looked like this:

tableA:([lname:`Dent`Beeblebrox`Prefect; fname:`Arthur`Zaphod`Ford]; iq:98 42 126; age: 23 49 78)

lname      fname | iq  age
-----------------| -------
Dent       Arthur| 98  23
Beeblebrox Zaphod| 42  49
Prefect    Ford  | 126 78

tableB:([lname:`Dent`Dirk; fname:`Arthur`Gently]; iq:105 118) <-- no age column

lname fname | iq 
------------| ---
Dent  Arthur| 105
Dirk  Gently| 118

Then the desired outcome of the update would be:

lname      fname | iq  age
-----------------| -------
Dent       Arthur| 105 23    <-- iq updated, age left alone
Beeblebrox Zaphod| 42  49
Prefect    Ford  | 126 78

* Dirk Gently NOT added because he didn't exist in tableA

Very grateful if anyone can give me an efficient syntax for updating a table this way.

Upvotes: 2

Views: 291

Answers (1)

Sean O&#39;Hagan
Sean O&#39;Hagan

Reputation: 1697

lj is what you want here:

q)tableA lj tableB
lname      fname | iq  age
-----------------| -------
Dent       Arthur| 105 23
Beeblebrox Zaphod| 42  49
Prefect    Ford  | 126 78

Upvotes: 2

Related Questions