【问题标题】:How to do full right outer join in kdb?如何在kdb中进行完全右外连接?
【发布时间】:2016-05-11 19:15:31
【问题描述】:

我有这两张桌子。

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 在那里。

【问题讨论】:

    标签: join outer-join kdb


    【解决方案1】:

    这应该可以满足您的需求:

    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
    

    【讨论】:

      【解决方案2】:

      这个怎么样?

      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 的 all 记录(在本例中为rrr),但这是一个术语问题。

      【讨论】:

        猜你喜欢
        • 2011-12-31
        • 2014-05-14
        • 2010-10-31
        • 1970-01-01
        • 2022-01-23
        • 2021-02-19
        • 2019-01-12
        • 2015-06-05
        • 1970-01-01
        相关资源
        最近更新 更多