CouchDB, how calculate two values in one view

Viewed 168

I have documents like these:

{"user": "A","year": "2016","price": 3,"litres": 19900}
{"user": "B","year": "2016","price": 2.3,"litres": 16000}
{"user": "C","year": "2016","price": 2.9,"litres": 15000}
{"user": "A","year": "2015","price": 4,"litres": 12030}
{"user": "B","year": "2015","price": 3,"litres": 20002}
{"user": "C","year": "2015","price": 2.9,"litres": 22000}

I need to calculate the sum of all values and the sum of one of the users by year, the goal is to compare a global average against one user.

For example, if I call the view with "A" as key to sum litres I need to get after reducing something like:

["A","2016"] {global: 50900, user: 19900}
["A","2015"] {global: 54032, user: 12030}

Thank you,

Diego

2 Answers
Related