jsvi
jsvi

Reputation: 3

Get total invoices per customer

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.

Upvotes: 0

Views: 67

Answers (1)

jsvi
jsvi

Reputation: 3

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.

Upvotes: 0

Related Questions