Kusto 中 table 的高级枢轴

Advanced pivot of table in Kusto

我最近才开始使用 Kusto,到目前为止一切顺利。 但是,我 运行 遇到了一个障碍,我在解决时遇到了一些问题。

我有以下table

datatable(name:string, gender:string, occupation: string)
[
    "John", "Male", "Janitor",
    "Sam", "Male", "Pilot",
    "Mary", "Female", "CEO"
]

我想旋转它所以我最终得到一个像这样的 table

datatable(info:string, John:string, Sam: string, Mary: string)
[
    "Name", "John", "Sam", "Mary",
    "Gender", "Male", "Male", "Female",
    "Occupation", "Janitor", "Pilot", "CEO"
]
| where info != "Name"

在这种情况下,名称将始终是唯一的,所以这不是问题。

这是我可悲的尝试之一:)

datatable(name:string, gender:string, occupation: string)
[
    "John", "Male", "Janitor",
    "Sam", "Male", "Pilot",
    "Mary", "Female", "CEO"
]
| summarize d = make_bag(
    pack(
        name, pack_array(gender, occupation))
) by name
| evaluate  bag_unpack(d)

这里有一个选项:

datatable(name:string, gender:string, occupation: string)
[
    "John", "Male", "Janitor",
    "Sam", "Male", "Pilot",
    "Mary", "Female", "CEO"
]
| as T
| summarize b = make_bag(pack(name, gender)) by info = "Gender"
| union (
    T
    | summarize b = make_bag(pack(name, occupation)) by info = "Occupation"
)
| evaluate bag_unpack(b)

此解决方案处理任意数量的列:

let my_data = datatable(name:string, gender:string, occupation: string)
[
    "John", "Male", "Janitor",
    "Sam", "Male", "Pilot",
    "Mary", "Female", "CEO"
];
my_data
| getschema 
| project variable = ColumnName, type = ColumnType, i = ColumnOrdinal, Dummy = 1
| where variable != 'name'
| join kind = inner (my_data | project array = pack_array(*), Dummy = 1) on Dummy
| project name = tostring(array[0]), variable = variable, type, value = array[i]
| evaluate pivot(name, any(value))

所有值列都转换为 'dynamic' 数据类型。尽管如此,为了保留数据类型元数据,添加了一个 'type' 列,用于保存输出 table.

中任何 的数据类型