{"id":3320,"date":"2016-03-07T13:17:37","date_gmt":"2016-03-07T13:17:37","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3320"},"modified":"2016-03-07T13:17:37","modified_gmt":"2016-03-07T13:17:37","slug":"3320-2","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/3320-2\/","title":{"rendered":"RethinkDB: Sum example"},"content":{"rendered":"<p>In RethinkDB, you can do a &#8216;sum&#8217; and a GROUP BY like so:<\/p>\n<pre lang=\"javascript\">r.db('test')\n .table('salaries')\n .group('role')\n .sum('salary')\n .ungroup()\n .map({Title: r.row('group'), Salary: r.row('reduction')})\n<\/pre>\n<p>If you just want an overall sum, do this:<\/p>\n<pre lang=\"javascript\">r.db('test')\n .table('salaries')\n .sum('salary')\n<\/pre>\n<table class=\"json_table\">\n<tbody>\n<tr class=\"jta_tr\">\n<td class=\"td_attr col-record col-record-attr\" data-col=\"record\"><\/td>\n<td class=\"td_attr col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-0\">Salary<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-1\" data-col=\"1\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-1\">Title<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr class=\"jta_tr\">\n<td class=\"col-record-td\" data-col=\"record\">\n<div class=\"jta_value col-record\">1<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-0 jta_num\">403000<\/div>\n<\/div>\n<\/td>\n<td class=\"col-1\" data-col=\"1\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-1 jta_null\">null<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr class=\"jta_tr\">\n<td class=\"col-record-td\" data-col=\"record\">\n<div class=\"jta_value col-record\">2<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-0 jta_num\">217000<\/div>\n<\/div>\n<\/td>\n<td class=\"col-1\" data-col=\"1\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-1 jta_string\">Designer<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<tr class=\"jta_tr\">\n<td class=\"col-record-td\" data-col=\"record\">\n<div class=\"jta_value col-record\">3<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-0 jta_num\">425000<\/div>\n<\/div>\n<\/td>\n<td class=\"col-1\" data-col=\"1\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-1 jta_string\">Manager<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div id=\"footer\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In RethinkDB, you can do a &#8216;sum&#8217; and a GROUP BY like so: r.db(&#8216;test&#8217;) .table(&#8216;salaries&#8217;) .group(&#8216;role&#8217;) .sum(&#8216;salary&#8217;) .ungroup() .map({Title: r.row(&#8216;group&#8217;), Salary: r.row(&#8216;reduction&#8217;)}) If you just want an overall sum, do this: r.db(&#8216;test&#8217;) .table(&#8216;salaries&#8217;) .sum(&#8216;salary&#8217;) Salary Title 1 403000 null 2 217000 Designer 3 425000 Manager<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[160,302,462,466,523],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3320"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3320"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3320\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}