amazon-athena - 汇总动态列的值并在新列中应用公式
问题描述
我有一个下表结构:
CREATE EXTERNAL TABLE IF NOT EXISTS `samplecontrolid`(
`controlId` string,
`status` string,
)
样本数据 :
controlid status
CloudTrail.1 PASSED
CloudTrail.1 PASSED
CloudTrail.1 PASSED
CloudTrail.1 PASSED
CloudTrail.1 FAILED
CloudTrail.1 FAILED
IAM.8 PASSED
IAM.8 PASSED
IAM.8 FAILED
IAM.8 FAILED
IAM.8 FAILED
IAM.8 FAILED
我想计算每个 ControlID 的通过和失败次数:
我写的查询:
SELECT
ControlId
,sum(case Status when 'PASSED' then 1 else 0 end) Passed
,sum(case Status when 'FAILED' then 1 else 0 end) Failed
from samplecontrolid
group by ControlId
输出:
ControlId Passed Failed
CloudTrail.1 4 2
IAM.4 4 3
IAM.8 2 4
CIS3.2 5 0
现在,我想要汇总通过/失败值的总列。由于通过/失败的列值是动态生成的,我不能直接在这些列上应用 Sum 函数。我如何即时添加这些值。如下所示:
ControlId Passed Failed Total
CloudTrail.1 4 2 6
IAM.4 4 3 7
IAM.8 2 4 6
CIS3.2 5 0 5
最后,计算一次总价值。我需要另一列计算百分比。
Percentage formula = Passed / Total * 100
Example for CloudTrail.1 = 4/6 * 100 = 66.66
最终输出:
ControlId Passed Failed Total Percentage
CloudTrail.1 4 2 6 66.66
IAM.4 4 3 7 57.14
IAM.8 2 4 6 33.33
CIS3.2 5 0 5 100
解决方案
推荐阅读
- java - 如何从本地java应用程序连接到kerberized hive
- angular - 杀死 Angular 服务人员并用另一个服务人员替换的最佳方法是什么?
- python - 将字符串中的最后两个字母大写
- docker - 带有 LocalDB 的 Visual Studio 2017 docker 容器
- jakarta-ee - 分离实体传递错误
- javascript - jquery $.when().done() 是否等待所有 $.ajax().done() 完成?
- javascript - 如何将 jQuery 的 insertBefore() 函数与 HTML 的 javascript 字符串一起使用?
- c++ - ReadProcessMemory CheatEngine 值
- r - integer64 的 fread 问题
- c# - Entity Framework Core 无法创建 DbSet,因为此类型未包含在上下文的模型中