0

I have a database where I have docs like customers (customerId, firstName, lastName, phoneNo, eMail) and invoice (invoiceId, invoiceDate, customerId).

I want to get the sum of invoices per customer in a view. I'm currently using Fauxton 2.0.

My view is currently:

function (doc) {
    if (doc.customerId && doc.invoiceId) {
        emit(doc.customerId, doc.invoiceId);
    }
}

But I want to add some sort of reduce function which can give me the total sum of invoices.

thanks.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
jsvi
  • 3
  • 4
  • Where is your attempt? What problems are you having? – Jonathan Hall Aug 03 '17 at 16:26
  • Well my map is function (doc) { emit(doc._id, {customerId: doc.customerId, invoiceId: doc.invoiceId} ) } - and then I type into my browser http://localhost:5984/assignment/_design/view5320/_view/sumofinv?key="the id of the first customer" - and then I'm not getting any rows at all. I have also added the _sum in my mapreduce function. – jsvi Aug 04 '17 at 07:27
  • Well, I solved the problem, it was just an issue of changing the reduce function _sum to _count, to get the total amount of invoices. So when requesting the key(customerId), I get the count. – jsvi Aug 04 '17 at 07:58

1 Answers1

0

Well, I solved the problem, it was just an issue of changing the reduce function _sum to _count, to get the total amount of invoices. So when requesting the key(customerId), I get the count.

jsvi
  • 3
  • 4