1

我正在加入 2 个表,它们都有数百个类似命名的列。我想更改每个表中的所有列名以包含表名。为了使查询简单,我不想显式地调用每个列名。有没有办法将表名附加到所有列名而不显式调用每一列?

例如:

let T1 = datatable (Key:string , Col2:string , Col3:string )[
"1", "b", "c",
"2", "e", "f",
"3", "h", "i"];
let T2 = datatable (Key:string , Col2:string , Col3:string )[
"1", "B", "C",
"2", "E", "F",
"4", "H", "I"];
T1 | join T2 on Key

结果:

Key Col2 Col3 Key1 Col21 Col31
1   b    c    1    B     C
2   e    f    2    E     F

期望的结果:

T1.Key T1.Col2 T1.Col3 T2.Key T2.Col2 T2.Col3
1      b       c       1      B       C
2      e       f       2      E       F
4

1 回答 1

3

如果列的顺序对您来说不重要,那么这将是一种方法:

let T1 = datatable (Key:string , Col2:string , Col3:string )
[
  "1", "b", "c",
  "2", "e", "f",
  "3", "h", "i"
] 
| project PackedRecord = todynamic(replace_regex(tostring(pack_all()), '"([a-zA-Z0-9_]*)":"', @'"T1_\1":"'))
| evaluate bag_unpack(PackedRecord);
let T2 = datatable (Key:string , Col2:string , Col3:string )
[
  "1", "B", "C",
  "2", "E", "F",
  "4", "H", "I"
] 
| project PackedRecord = todynamic(replace_regex(tostring(pack_all()), '"([a-zA-Z0-9_]*)":"', @'"T2_\1":"'))
| evaluate bag_unpack(PackedRecord);
let JoinTable = T1 | join kind=inner T2 on $left.T1_Key == $right.T2_Key;
JoinTable

结果:

T1_Col2 T1_Col3 T1_Key T2_Col2 T2_Col3 T2_Key
b C 1 C 1
e F 2 F 2

如果要对列重新排序,可以使用project-reorder

于 2021-11-07T08:53:52.007 回答