ARRAY_SUM 有条件
ARRAY_SUM with condition
我有这样的文档
{
bills: [
{
id: "1",
total: 20.0
},
{
id: "1",
total: 20.0
},
{
id: "2",
total: 10.0
}
]
}
我想根据 id 属性 对总价值进行 DISTINCT SUM,但找不到这种情况的说明。
对于示例案例,预期总数为 30.0。
使用 ARRAY 运算符 select 要使用数组的哪些元素。
https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/collectionops.html#array
然后使用 ARRAY_DISTINCT() 和 ARRAY_SUM() 计算总数。
https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/arrayfun.html#fn-array-distinct
https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/arrayfun.html#fn-array-sum
“id”:“1”取值不同,你选哪个
{
bills: [
{
id: "1",
total: 20.0
},
{
id: "1",
total: 30.0
},
{
id: "2",
total: 10.0
}
]
}
通过使用子查询表达式 https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/subqueries.html 您可以使用数组的完整 select 功能。
如果值不同,以下查询使用 MAX。这是每个文件的总和
SELECT ARRAY_SUM((SELECT RAW MAX(b.total) FROM d.bills AS b GROUP BY b.id)) AS s
FROM default AS d
WHERE ......
我有这样的文档
{
bills: [
{
id: "1",
total: 20.0
},
{
id: "1",
total: 20.0
},
{
id: "2",
total: 10.0
}
]
}
我想根据 id 属性 对总价值进行 DISTINCT SUM,但找不到这种情况的说明。
对于示例案例,预期总数为 30.0。
使用 ARRAY 运算符 select 要使用数组的哪些元素。
https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/collectionops.html#array
然后使用 ARRAY_DISTINCT() 和 ARRAY_SUM() 计算总数。
https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/arrayfun.html#fn-array-distinct https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/arrayfun.html#fn-array-sum
“id”:“1”取值不同,你选哪个
{
bills: [
{
id: "1",
total: 20.0
},
{
id: "1",
total: 30.0
},
{
id: "2",
total: 10.0
}
]
}
通过使用子查询表达式 https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/subqueries.html 您可以使用数组的完整 select 功能。
如果值不同,以下查询使用 MAX。这是每个文件的总和
SELECT ARRAY_SUM((SELECT RAW MAX(b.total) FROM d.bills AS b GROUP BY b.id)) AS s
FROM default AS d
WHERE ......