我正在尝试制定一个 AQL 查询来聚合大型数据集的多个属性的通过/失败计数。我有一组测试数据,看起来像这样:
[
{
workorder: "123456",
device_id: "1",
pull_force: "PASS",
cut_force: "PASS",
kpa_force: "FAIL",
...
}
]
我想从集合返回的输出类似于:
{
pull_force: {
PASS: 300321,
FAIL: 400
},
cut_force: {
PASS: 300211,
FAIL: 200
},
...
}
我可以收集我正在寻找的运行下面的数据,但感觉不对,因为我正在寻找相当多的通过/失败属性,所以运行速度非常慢:
LET pull_force_data = mydataset[*].pull_force
LET cut_force_data = mydataset[*].cut_force
LET kpa_force_data = mydataset[*].kpa_force
...
RETURN {
pull_force: {
PASS: pull_force_data[* FILTER CURRENT == "PASS"],
FAIL: pull_force_data[* FILTER CURRENT == "FAIL"]
},
cut_force: {
PASS: cut_force_data[* FILTER CURRENT == "PASS"],
FAIL: cut_force_data[* FILTER CURRENT == "FAIL"]
},
kpa_force: {
PASS: kpa_force_data[* FILTER CURRENT == "PASS"],
FAIL: kpa_force_data[* FILTER CURRENT == "FAIL"]
},
...
}
有人可以给我一些指导吗?
使用收集:
COLLECT操作可以将数据按一个或多个分组进行分组 标准,检索所有不同的值,计算值出现的频率, 并有效计算统计属性
如果您要聚合所有文档,不需要特定的组密钥,则可以使用简单的
Aggregate
:
FOR doc IN collection
COLLECT
pull_force = doc.pull_force
//rest...
AGGREGATE
pull_force_PASS = SUM(pull_force == "PASS" ? 1 : 0),
pull_force_FAIL = SUM(pull_force == "FAIL" ? 1 : 0)
//rest...
RETURN
{
pull_force: {
PASS: pull_force_PASS,
FAIL: pull_force_FAIL
}
//rest...
}