我有一系列购买交易:
{....,"userId": "1","amount":3,....}
{....,"userId": "1","amount":1,....}
{....,"userId": "2","amount":4,....}
{....,"userId": "1","amount":2,....}
我需要使用最大购买金额和总和来打印userID。
使用map reduce获取购买总额没有问题:
function map(){
emit(this.userID,{amount:this.amount});
}
然后减少:
function reduce(key,value){
var sum = 0;
for ( var i=0; i<value.length; i++ ) {
sum += value[i].amount;
}
return sum;
}
我知道我需要使用finalize函数来选择最大的结果并将其作为单个集合返回 如何使用finalize函数选择最大的结果? 你能帮忙吗?
更新
就SQL而言,它应该是这样的:
select MAX(tbl.amn), tbl.uid from
(select o, userID as uid, SUM(o.amount) as amn from order o GROUP BY o.userID ) as tbl GROUP BY tbl.uid
答案 0 :(得分:1)