Join 如何在kdb中进行完全右外连接?
我有这两张桌子Join 如何在kdb中进行完全右外连接?,join,outer-join,kdb,Join,Outer Join,Kdb,我有这两张桌子 tab1:([]col1:`abc`def`ghi;col2:2 4 6); tab2:([]col1:`def`ghi`ghi`rrr;col3:5 10 11 15); 我希望将所有内容都保留在正确的表中,但复制了col1以匹配tab2中的col1。我找到的最近的是ij tab2 ij 1! tab1 col1 col3 col2 -------------- def 5 4 ghi 10 6 ghi 11 6 然而,我想得出以
tab1:([]col1:`abc`def`ghi;col2:2 4 6);
tab2:([]col1:`def`ghi`ghi`rrr;col3:5 10 11 15);
我希望将所有内容都保留在正确的表中,但复制了col1以匹配tab2中的col1。我找到的最近的是ij
tab2 ij 1! tab1
col1 col3 col2
--------------
def 5 4
ghi 10 6
ghi 11 6
然而,我想得出以下结果:
col1 col3 col2
--------------
abc 2
def 5 4
ghi 10 6
ghi 11 6
如果tab2的col1中还有其他值,我不想把它放到结果表中:就像我不想把'rrr'放在那里一样。这应该会给出您想要的:
q){x,flip y}/[tab1 lj `col1 xgroup tab2]
col1 col2 col3
------------------
abc 2 `long$()
def 4 5
ghi 6 10
ghi 6 11
没有经过严格测试,但这是一个起点
编辑:事实上,它比这更微妙一点。当col3中有一个空白列表时,翻转会导致问题,上面的例子只是碰巧避免了这个问题
您可能需要这样的东西来捕捉边缘案例:
q){x,$[0=count f:flip y;enlist first each y;f]}/[();tab1 lj `col1 xgroup tab2]
col1 col2 col3
--------------
abc 2
def 4 5
ghi 6 10
ghi 6 11
这个怎么样
q)uj[select from tab1 where not col1 in exec col1 from tab2;tab2 ij 1!tab1]
col1 col2 col3
--------------
abc 2
def 4 5
ghi 6 10
ghi 6 11
这里我们将联合应用于a)从tab1
到tab2
中不存在的所有内容,以及b)从tab1
到tab2
中都存在的所有内容
不确定是否可以调用此完全外部右连接,因为我们不希望包含tab2中的所有记录(rrr
,在本例中),但这是一个术语问题