列连接变量列数和重新排列

时间:2014-11-17 20:23:20

标签: kdb

我希望逐行加入以下3个表,但也要移动一些列。

 q)
newtab1:([]basecol1:10 20 30;basecol2:40 50 60;L1Col1:"a","b","c";L1Col2:"d","e","f";L1Col3:"g","h","i")
newtab2:([]basecol1:100 200 300;basecol2:400 500 600;L1Col1:"a","b","c";L1Col2:"d","e","f";L2Col1:"j","k","l";L2Col2:"m","n","o";L1Col3:"g","h","i";L2Col3:"p","q","r")
newtab3:([]basecol1:1 2 3;basecol2:4 5 6;L1Col1:"a","b","c";L1Col2:"d","e","f";L2Col1:"j","k","l";L2Col2:"m","n","o";L3Col1:"s","t","u";L3Col2:"v","w","x";L1Col3:"g","h","i";L2Col3:"p","q","r";L3Col3:"y","z","z")
q)newtab3

basecol1 basecol2 L1Col1 L1Col2 L2Col1 L2Col2 L3Col1 L3Col2 L1Col3 L2Col3 L3Col3
--------------------------------------------------------------------------------
1        4        a      d      j      m      s      v      g      p      y
2        5        b      e      k      n      t      w      h      q      z
3        6        c      f      l      o      u      x      i      r      z

也就是说,每个“L”的“Col3”应移到最后

我想用零填充所有没有数据的列。也就是说,我需要向表1添加3列(并移动一列),将6列添加到表2(并移动2列)。列始终使用相同的模式标记。 我开始创建像这样的列名:

LegColNames:("Col1";"Col2";"Col3")
L1LegColNames: "L1",/:legColNames

但是这使它们成为一个字符串,我如何将它们作为列名?

FWIW,在实际实现中,这将加入数百个表,我需要获取具有最大Ls数的表,并为每个表添加适当数量的零列以使其相等最大的。 它可以高达L13。

2 个答案:

答案 0 :(得分:1)

始终将这些事情分解为步骤:

q)t:uj/[(newtab1;newtab2;newtab3)]  / join all tables

q)t2:0^t / this would fill the number columns
q)t2:flip {$[10h=type x;"0"^x;x]} each flip t / fill character columns with zeros

q){c:cols x; (c iasc c like "*Col3") xcols x}t2 / pull our columns to the end

basecol1 basecol2 L1Col1 L1Col2 L2Col1 L2Col2 L3Col1 L3Col2 L1Col3 L2Col3 L3Col3
--------------------------------------------------------------------------------
10       40       a      d      0      0      0      0      g      0      0
20       50       b      e      0      0      0      0      h      0      0
30       60       c      f      0      0      0      0      i      0      0
100      400      a      d      j      m      0      0      g      p      0
200      500      b      e      k      n      0      0      h      q      0
300      600      c      f      l      o      0      0      i      r      0
1        4        a      d      j      m      s      v      g      p      y
2        5        b      e      k      n      t      w      h      q      z
3        6        c      f      l      o      u      x      i      r      z

最后一部分是对于更多表格来说更难的一点。您将要解析结束编号,然后找到列的已排序索引。

  • 赖安

答案 1 :(得分:0)

我开始认为你的字段不是char类型而是字符串类型(char列表列表),因为你指出""是字段的值之一。如果是这种情况,请使用@Ryan的建议:

q)newtab1:([]basecol1:string 10 20 30;basecol2:string 40 50 60;L1Col1:string 3?`2;L1Col2:string 3?`2;L1Col3:string 3?`2)
q)newtab2:([]basecol1:string 100 200 300;basecol2:string 400 500 600;L1Col1:string 3?`2;L1Col2:string 3?`2;L2Col1:string 3?`2;L2Col2:string 3?`2;L1Col3:string 3?`2;L2Col3:string 3?`2)
q)newtab3:([]basecol1:string 1 2 3;basecol2:string 4 5 6;L1Col1:string 3?`2;L1Col2:string 3?`2;L2Col1:string 3?`2;L2Col2:string 3?`2;L3Col1:string 3?`2;L3Col2:string 3?`2;L1Col3:string 3?`2;L2Col3:string 3?`2;L3Col3:string 3?`2)
q)t:uj/[(newtab1;newtab2;newtab3)]

q)@[`t;cols t;{?[x like "";count[x]#enlist 1#"0";x]}]      / using vector condition

q){c:cols x; (c iasc c like "*Col3") xcols x}t
basecol1 basecol2 L1Col1 L1Col2 L2Col1 L2Col2 L3Col1 L3Col2 L1Col3 L2Col3 L3Col3
--------------------------------------------------------------------------------
"10"     "40"     "do"   "ma"   ,"0"   ,"0"   ,"0"   ,"0"   "gm"   ,"0"   ,"0"
"20"     "50"     "nc"   "bo"   ,"0"   ,"0"   ,"0"   ,"0"   "kp"   ,"0"   ,"0"
"30"     "60"     "df"   "mn"   ,"0"   ,"0"   ,"0"   ,"0"   "ec"   ,"0"   ,"0"
"100"    "400"    "ll"   "fp"   "ml"   "ce"   ,"0"   ,"0"   "ch"   "ho"   ,"0"
"200"    "500"    "cn"   "ol"   "fm"   "ij"   ,"0"   ,"0"   "bo"   "of"   ,"0"
"300"    "600"    "pk"   "pf"   "ii"   "ap"   ,"0"   ,"0"   "ed"   "ii"   ,"0"
,"1"     ,"4"     "jo"   "dh"   "cm"   "om"   "oa"   "ak"   "ap"   "no"   "kj"
,"2"     ,"5"     "kg"   "mh"   "ll"   "jk"   "nn"   "og"   "nn"   "gc"   "ig"
,"3"     ,"6"     "nh"   "mg"   "ob"   "fl"   "mi"   "lf"   "am"   "jo"   "nf"

0^"0"^不适用于空字符串:

q)"0"^""
""