开发者

How to get SUM of all CouchDB values?

How to extract SUM depending on ID or something, with goal of getting SUM of all values?

I am able to get all values but canot SUM beacouse its a single variable, so there must be some kind of segmentation to SUM them?? any ideas?

$db = $.couch.db("fuel");  

function refreshFuel() {  

$("div#fuel").empty();  

$db.view("fuel/bill", {success: function(data) {  

    for (i in data.rows) {  
    id = data.rows[i].id;  
    bill = data.rows[i].key;  
    date = data.rows[i].value;  

    html = '<div class="fuel">' +  
   '<span class="bill">' + bill + '</span> ' 开发者_StackOverflow社区+  
   '<span class="date">' + date + '</span> ' +    
   '</div>';  

  $("div#fuel").append(html);  
 }  
 }});  
}  

function getAll(){

$("div#all").empty();  

$db.view("fuel/bill", {success: function(data) {  

    for (i in data.rows) {  
    id = data.rows[i].id;  
    bill = data.rows[i].key;  // how to get SUM of all this values
    date = data.rows[i].value;  

    html = 
   '<span class="all">' + bill + '</span> '     


  $("div#all").append(html);  
 }  
}});

}

EDIT:

my JSON looks like this:

    {
   "_id": "1a06982d3434f37a04a02dbf360010ee",
   "_rev": "1-c8c9d5252a76abd8f565ff82bf63b0e8",
   "value": "200",
   "date": "2011-09-05T10:28:55.101Z"
}

and my VIEW look like this:

function(doc) {
    if (doc.value, doc.date) {
        emit(doc.value,doc.date, doc);
    }    
}


You just need to add a reduce function, e.g,

function(keys, values, rereduce) {
  return sum(values);
}

This is so common that we have a built-in function that's much faster;

_sum

You'll need to emit the value you want to sum as the value, so something like;

function(doc) {
  emit(doc.date, doc.value);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜