给定以下数据模型:
{
"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的积分总数?即:
{
{
"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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句