RethinkDB: Average example

You can easily get the average of values in a table in RethinkDB:

r.db('test')
 .table('salaries')
 .avg('salary')

If you want to do a GROUP BY as well, you’ll need to do a little more effort:

r.db('test')
 .table('salaries')
 .group('role')
 .avg('salary')
 .ungroup()
 .map(
    {Title: r.row('group'), 
     Amount: r.row('reduction')})
 

 

group
reduction
1
null
80600
2
Designer
72333.33333333333
3
Manager
106250

Interested in JavaScript? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *