gpt4 book ai didi

子文档中的 couchbase N1QL group-by

转载 作者:行者123 更新时间:2023-12-04 08:24:42 24 4
gpt4 key购买 nike

鉴于以下数据模型:

{
"events": [
{
"customerId": "a",
"type": "credit" ,
"value": 10
},
{
"customerId": "a",
"type": "credit" ,
"value": 10
},
{
"customerId": "b",
"type": "credit" ,
"value": 5
},
{
"customerId": "b",
"type": "credit" ,
"value": 5
}
]
}
如何通过 customerId 查询信用总额? IE:
{
{
"customerId": "a",
"total": "20
},
{
"customerId": "b",
"total": "10
}
}

最佳答案

每个文档聚合使用 SUBQUERY 表达式

SELECT d.*, 
(SELECT e.customerId, SUM(e.`value`) AS total
FROM d.events AS e
WHERE ......
GROUP BY e.customerId) AS events
FROM default AS d
WHERE ...........;
对于整个查询
SELECT e.customerId, SUM(e.`value`) AS total
FROM default AS d
UNNEST d.events AS e
WHERE ......
GROUP BY e.customerId;

关于子文档中的 couchbase N1QL group-by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65325172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com