azure KQL -获取动态列的值总和

zzzyeukh  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(91)

从一个查询中,我得到一个结果,其中有三个列:服务、响应代码和列

Service StatusCode  Count
A       201             100
A       202             50
A       503             20
C       201             25

响应代码是动态的,不是固定的。
我希望每个服务将数据转换为一行,每列表示StatusCode,然后我还需要另外两列--DataStatusCount和FailStatusCount,它们可以根据条件对列的值求和。示例输出:

Service 201 202 503  SuccessStatusCount   FailStatusCount
A       100 50  20   150//(100+50)        20        
C       25  0   0    25                   0

我可以使用pivot得到前4列:

let x = datatable (Service:string, ResponseCode: long, Count: long)
    [
    "A",201,100,
    "A",202,50,
    "A",503,20,
    "C",201,25
    ];
x | evaluate pivot(ResponseCode, sum(Count))

但是,我如何才能获得故障状态计数和故障状态计数?
我不想做| extend SuccessStatusCount = 201+202+203+....,因为StatusCodes是动态的,数量很大。我只知道StatusCount应该包括StatusCode >=200且<500的所有调用的计数
我怎么才能得到它?

2skhul33

2skhul331#

这里有一个实现这一目标选项:

let T = datatable (Service:string, ResponseCode: long, Count: long)
[
  "A",201,100,
  "A",202,50,
  "A",503,20,
  "C",201,25
];
let totals = toscalar(
    T
    | extend what = case(ResponseCode between(200..299), "Success", "Failure")
    | summarize sum(Count) by Service, what
    | summarize make_bag(bag_pack(strcat(Service, "_", what),sum_Count))
);
T
| evaluate pivot(ResponseCode, sum(Count))
| extend SuccessStatusCount = coalesce(tolong(totals[strcat(Service, "_Success")]), 0),
         FailureStatusCount = coalesce(tolong(totals[strcat(Service, "_Failure")]), 0)

| 服务| 201 | 202 | 503 |客户端状态计数|FailureStatusCount|
| --|--|--|--|--|--|
| 一| 100 | 50 | 20 | 150 | 20 |
| C| 25 | 0 | 0 | 25 | 0 |

相关问题