azure-log-analytics - 库斯托 : How to unpivot - turn columns into rows?

标签 azure-log-analytics azure-data-explorer kql

使用帮助集群上 Samples 数据库中的 StormEvents 表:

StormEvents
| where State startswith "AL"
| where EventType has "Wind"
| where StartTime == "2007-01-02T02:16:00Z"
| project StartTime, State, EventType, InjuriesDirect, InjuriesIndirect, DeathsDirect, DeathsIndirect

我想要表单的基于行的输出:

desired un-pivoted output

我看到了 pivot() 函数,但它似乎只向另一个方向发展,从行到列。

我一直在尝试各种 pack() 想法,但似乎无法获得所需的输出。

例子:

StormEvents
| where State startswith "AL"
| where EventType has "Wind"
| where StartTime == "2007-01-02T02:16:00Z"
| project StartTime, State, EventType, InjuriesDirect, InjuriesIndirect, DeathsDirect, DeathsIndirect
| extend Packed =   pack(
                    "CasualtyType", "InjuriesDirect", "CasualtyCount", InjuriesDirect,
                    "CasualtyType", "InjuriesIndirect", "CasualtyCount", InjuriesIndirect,
                    "CasualtyType", "DeathsDirect", "CasualtyCount", DeathsDirect,
                    "CasualtyType", "DeathsIndirect", "CasualtyCount", DeathsIndirect
                )
| project-away InjuriesDirect, InjuriesIndirect, DeathsDirect, DeathsIndirect
| mv-expand Packed

这给了我太多行,我不清楚如何将它们转换为列。

用于所需输出的正确模式是什么?

最佳答案

您可以尝试以下方法:

let casualty_types = dynamic(["InjuriesDirect", "DeathsDirect", "InjuriesIndirect", "DeathsIndirect"]);
StormEvents
| where State startswith "AL"
| where EventType has "Wind"
| where StartTime == "2007-01-02T02:16:00Z"
| project StartTime, State, EventType, properties = pack_all()
| mv-apply casualty_type = casualty_types to typeof(string) on (
    project casualty_type, casualty_count = tolong(properties[casualty_type])
)
| project-away properties

关于azure-log-analytics - 库斯托 : How to unpivot - turn columns into rows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58820720/

相关文章:

azure - 获取 Azure 数据工厂日志

azure - Kustos bag_unpack 不解压我的 json 字典字符串

azure - 具有时间范围的 Log Analytics KQL 无法通过使用 "workspace"的 App Insights 工作

azure - 将本地 Docker 日志获取到 Azure 日志分析

azure - 自动触发安全中心 Playbook

azure - 为什么 tolong 不能处理来自 strcat 的字符串?

Azure Sentinel 基于来自 KQL 的 5 分钟数据间隔发出警报

azure-data-explorer - 如果列名在数据库中包含 'teams' 并返回所有表名和列名,如何使用 Kusto 查询语言查找?

azure-data-explorer - Kusto/KQL 查询获取不同的输出,然后在后续查询中使用

Azure API 管理记录请求和响应正文