Kdb Q:仅在空行上左连接

时间:2019-01-14 18:28:02

标签: join kdb

仅当t1t2侧为空时,我想在列id2的两个表id2t1之间执行左联接。
作为一名qbie,我想知道它是否比下面的解决方案更好?

t1: ([] id1:`AAA`BBB`CCC`DDD`EEE; id2:```02C``E25)
t2: ([] id1:`AAA`BBB`CCC`DDD`SSS; id2:`02A`02B`C2C`DD0`SPE)

预期结果:

([] id1:`AAA`BBB`CCC`DDD`EEE; id2:`02A`02B`02C`DD0`E25)

id1 id2
-------
AAA 02A
BBB 02B
CCC 02C
DDD DD0
EEE E25

到目前为止,我的解决方案是通过将列id2id3更改为t2来执行左联接,然后应用向量条件函数?:< / p>

t1:t1 lj 1!select id1, id3:id2 from t2
t1[`id2]: ?[null t1[`id2];t1[`id3];t1[`id2]]
t1:delete id3 from t1

谢谢!

3 个答案:

答案 0 :(得分:1)

使用填充^的另一种方法:

q){key[x]#y^x}[1!t1;1!t2]
id1| id2
---| ---
AAA| 02A
BBB| 02B
CCC| 02C
DDD| DD0
EEE| E25

答案 1 :(得分:0)

下面的一个划线员在表t2上键入关键字,然后通过将t1的id1列的表传递给该表来索引该表并选择id2值。 然后执行一次fill(^),仅在t1的id2为空的情况下采用此值。

q)t1: ([] id1:`AAA`BBB`CCC`DDD`EEE; id2:```02C``E25)
q)t2: ([] id1:`AAA`BBB`CCC`DDD`SSS; id2:`02A`02B`C2C`DD0`SPE)
q)update (1!t2)[([]id1);`id2]^id2 from t1
id1 id2
-------
AAA 02A
BBB 02B
CCC 02C
DDD DD0
EEE E25

答案 2 :(得分:0)

您可以简单地过滤掉t2中t1中为空的符号的行,并在此新表上进行左连接。如果您的表有许多要连接的列,这也很有用。

q) t1 lj 1! select from t2 where id1 in exec id1 from t1 where id2=`