[英]Advanced pivot of table in Kusto
I've just recently started using Kusto and so far so good.我最近才开始使用 Kusto,到目前为止还不错。 However, I've run into a roadblock which I'm having some issues in solving.
但是,我遇到了一个障碍,我正在解决一些问题。
I have the following table我有下表
datatable(name:string, gender:string, occupation: string)
[
"John", "Male", "Janitor",
"Sam", "Male", "Pilot",
"Mary", "Female", "CEO"
]
And I want to pivot it so I end up with a table like this我想旋转它 所以我最终得到了一张这样的桌子
datatable(info:string, John:string, Sam: string, Mary: string)
[
"Name", "John", "Sam", "Mary",
"Gender", "Male", "Male", "Female",
"Occupation", "Janitor", "Pilot", "CEO"
]
| where info != "Name"
The names in this case will always be unique, so that's not an issue.在这种情况下,名称将始终是唯一的,因此这不是问题。
Here is one of my pathetic attempts :)这是我可悲的尝试之一:)
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)
here's one option:这是一种选择:
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)
This solution handles any number of columns:此解决方案处理任意数量的列:
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))
All value columns are cast to 'dynamic' data type.所有值列都转换为“动态”数据类型。 To nevertheless retain the data type metadata, a 'type' column is added that holds the data type for any row in the output table.
尽管如此,为了保留数据类型元数据,添加了一个“类型”列,用于保存输出表中任何行的数据类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.