Search:

Search all manuals
Search this manual
Manual
Couchbase Server Manual 2.0
Community Wiki and Resources
Download Couchbase Server 2.0
Couchbase Developer Guide 2.0
Client Libraries
Couchbase Server Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
9.10 Translating SQL to Map/Reduce
Chapter Sections
Chapters

9.10.4. Translating SQL GROUP BY to Map/Reduce

The GROUP BY parameter within SQL provides summary information for a group of matching records according to the specified fields, often for use with a numeric field for a sum or total value, or count operation.

For example:

SELECT name,city,SUM(sales) FROM sales GROUP BY name,city

This query groups the information by the two fields 'name' and 'city' and produces a sum total of these values. To translate this into a map/reduce function within Couchbase Server:

For example, in the above case, the corresponding map function can be written as map():

Javascript
function(doc, meta) 
{
   emit([doc.name,doc.city],doc.sales);
}

This outputs the name and city as the key, and the sales as the value. Because the SUM() function is used, the built-in reduce() function _sum can be used.

An example of this map/reduce combination can be seen in Section 9.5.2.2, “Built-in _sum.

More complex grouping operations may require a custom reduce function. For more information, see Section 9.5.2.4, “Writing Custom Reduce Functions”.