pom
pom

Reputation: 340

KDB: how do I insert/upsert rows from one table to another

k1:([a:("ff"; "yy"; "zz");z:("tt"; "yy"; "hh")] b:("33"; "44"; "55"); c:("66"; "77"; "88"))


k2:([z:()] a:(); b:(); c:(); m:(); i:())

k1 and k2 are keyed tables. k2 has all the columns of k1 and more.

How do I perform a conditional upsert from k1 to k2. Basically, what is the corresponding working statement of the one below:

`k2 upsert select k1 where a="ff"

Upvotes: 2

Views: 1367

Answers (2)

emc211
emc211

Reputation: 1379

Your attempted statement is actually very close. You can use:

`k2 upsert 0!select from k1 where a like "ff"

Upvotes: 2

Joe Griffiths
Joe Griffiths

Reputation: 381

I think this will solve your issue:

k2 uj `z xkey select from k1 where a like "ff"

the xkey will maintain the key on k2 while the uj will maintain all of the columns, including those without values in k1.

In this example this would return:

z   | a    b    c    m i
----| ------------------
"tt"| "ff" "33" "66"

Upvotes: 7

Related Questions