{"id":3323,"date":"2016-03-08T02:41:48","date_gmt":"2016-03-08T02:41:48","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3323"},"modified":"2016-03-08T02:41:48","modified_gmt":"2016-03-08T02:41:48","slug":"rethinkdb-group-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/","title":{"rendered":"RethinkDB: GROUP BY example"},"content":{"rendered":"<p>If you add a &#8216;group&#8217; to a RethinkDB query, it will behave similarly to a similar query in SQL, except that you need to &#8216;ungroup&#8217; it afterward:<\/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'), Amount: r.row('reduction')})\n<\/pre>\n<div id=\"sticky-wrap\">\n<div id=\"main-container\">\n<div id=\"cluster\" class=\"container\">\n<div id=\"dataexplorer\">\n<div class=\"section\">\n<div class=\"results_container\">\n<div class=\"result_view\">\n<div class=\"tab-content\">\n<div class=\"results table_view_container\">\n<div class=\"results_wrap table_view\">\n<div class=\"json_table_container\">\n<table class=\"json_table\">\n<tbody>\n<tr class=\"jta_tr\">\n<td class=\"td_attr col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-0\">Amount<\/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>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"footer\"><\/div>\n<p>If, however, you group by two items, the grouping will come out as an array, rather than individual, named columns:<\/p>\n<pre lang=\"javascript\">r.db('test')\n .table('salaries')\n .group('role', 'level')\n .sum('salary')\n .ungroup()<\/pre>\n<p>7 rows returned in 61ms.<\/p>\n<div id=\"sticky-wrap\">\n<div id=\"main-container\">\n<div id=\"cluster\" class=\"container\">\n<div id=\"dataexplorer\">\n<div class=\"section\">\n<div class=\"results_container\">\n<div class=\"result_view\">\n<div class=\"results_header\"><\/div>\n<div class=\"tab-content\">\n<div class=\"results table_view_container\">\n<div class=\"results_wrap table_view\">\n<div class=\"json_table_container\">\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\">group<\/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\">reduction<\/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=\"value-0 jta_value \">\n<div class=\"json_tree\"><span class=\"jt_b\">[<\/span><\/p>\n<ul class=\"jt_array jt_collapsible\">\n<li><span class=\"jt_editable jt_null\">null<\/span> ,<\/li>\n<li><span class=\"jt_editable jt_null\">null<\/span><\/li>\n<\/ul>\n<p><span class=\"jt_b jt_close\">]<\/span><\/div>\n<\/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_num\">403000<\/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=\"value-0 jta_value \">\n<div class=\"json_tree\"><span class=\"jt_b\">[<\/span><\/p>\n<ul class=\"jt_array jt_collapsible\">\n<li><span class=\"jt_editable jt_string\">&#8220;Designer&#8221;<\/span> ,<\/li>\n<li><span class=\"jt_editable jt_string\">&#8220;1&#8221;<\/span><\/li>\n<\/ul>\n<p><span class=\"jt_b jt_close\">]<\/span><\/div>\n<\/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_num\">61000<\/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=\"value-0 jta_value \">\n<div class=\"json_tree\"><span class=\"jt_b\">[<\/span><\/p>\n<ul class=\"jt_array jt_collapsible\">\n<li><span class=\"jt_editable jt_string\">&#8220;Designer&#8221;<\/span> ,<\/li>\n<li><span class=\"jt_editable jt_string\">&#8220;2&#8221;<\/span><\/li>\n<\/ul>\n<p><span class=\"jt_b jt_close\">]<\/span><\/div>\n<\/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_num\">71000<\/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\">4<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"value-0 jta_value \">\n<div class=\"json_tree\"><span class=\"jt_b\">[<\/span><\/p>\n<ul class=\"jt_array jt_collapsible\">\n<li><span class=\"jt_editable jt_string\">&#8220;Designer&#8221;<\/span> ,<\/li>\n<li><span class=\"jt_editable jt_string\">&#8220;3&#8221;<\/span><\/li>\n<\/ul>\n<p><span class=\"jt_b jt_close\">]<\/span><\/div>\n<\/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_num\">85000<\/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\">5<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"value-0 jta_value \">\n<div class=\"json_tree\"><span class=\"jt_b\">[<\/span><\/p>\n<ul class=\"jt_array jt_collapsible\">\n<li><span class=\"jt_editable jt_string\">&#8220;Manager&#8221;<\/span> ,<\/li>\n<li><span class=\"jt_editable jt_string\">&#8220;13&#8221;<\/span><\/li>\n<\/ul>\n<p><span class=\"jt_b jt_close\">]<\/span><\/div>\n<\/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_num\">81000<\/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\">6<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"value-0 jta_value \">\n<div class=\"json_tree\"><span class=\"jt_b\">[<\/span><\/p>\n<ul class=\"jt_array jt_collapsible\">\n<li><span class=\"jt_editable jt_string\">&#8220;Manager&#8221;<\/span> ,<\/li>\n<li><span class=\"jt_editable jt_string\">&#8220;2&#8221;<\/span><\/li>\n<\/ul>\n<p><span class=\"jt_b jt_close\">]<\/span><\/div>\n<\/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_num\">97000<\/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\">7<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"value-0 jta_value \">\n<div class=\"json_tree\"><span class=\"jt_b\">[<\/span><\/p>\n<ul class=\"jt_array jt_collapsible\">\n<li><span class=\"jt_editable jt_string\">&#8220;Manager&#8221;<\/span> ,<\/li>\n<li><span class=\"jt_editable jt_string\">&#8220;3&#8221;<\/span><\/li>\n<\/ul>\n<p><span class=\"jt_b jt_close\">]<\/span><\/div>\n<\/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_num\">247000<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"footer\"><\/div>\n<p>If this bothers you, and you want to make the result look like the output of SQL, you can add a mapping function to turn it back into rows, like so:<\/p>\n<pre lang=\"javascript\"> .table('salaries')\n .group('role', 'level')\n .sum('salary')\n .ungroup()\n .map( \n   (x) =&gt; {\n     return {\n       Title: x('group')(0),\n       Level: x('group')(1),\n       Value: x('reduction')\n     }\n   } \n )\n<\/pre>\n<p>7 rows returned in 54ms.<\/p>\n<div id=\"sticky-wrap\">\n<div id=\"main-container\">\n<div id=\"cluster\" class=\"container\">\n<div id=\"dataexplorer\">\n<div class=\"section\">\n<div class=\"results_container\">\n<div class=\"result_view\">\n<div class=\"results_header\"><\/div>\n<div class=\"tab-content\">\n<div class=\"results table_view_container\">\n<div class=\"results_wrap table_view\">\n<div class=\"json_table_container\">\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\">Level<\/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<td class=\"td_attr col-2\" data-col=\"2\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-2\">Value<\/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_null\">null<\/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<td class=\"col-2\" data-col=\"2\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-2 jta_num\">403000<\/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_string\">1<\/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<td class=\"col-2\" data-col=\"2\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-2 jta_num\">61000<\/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_string\">2<\/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<td class=\"col-2\" data-col=\"2\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-2 jta_num\">71000<\/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\">4<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-0 jta_string\">3<\/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<td class=\"col-2\" data-col=\"2\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-2 jta_num\">85000<\/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\">5<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-0 jta_string\">13<\/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<td class=\"col-2\" data-col=\"2\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-2 jta_num\">81000<\/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\">6<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-0 jta_string\">2<\/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<td class=\"col-2\" data-col=\"2\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-2 jta_num\">97000<\/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\">7<\/div>\n<\/td>\n<td class=\"col-0\" data-col=\"0\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-0 jta_string\">3<\/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<td class=\"col-2\" data-col=\"2\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-2 jta_num\">247000<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"footer\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you add a &#8216;group&#8217; to a RethinkDB query, it will behave similarly to a similar query in SQL, except that you need to &#8216;ungroup&#8217; it afterward: 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;), Amount: r.row(&#8216;reduction&#8217;)}) Amount Title 1 403000 null 2 217000 Designer 3 425000 Manager If, however, you group by two items, the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;RethinkDB: GROUP BY example&#8221;<\/span><\/a><\/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,"footnotes":""},"categories":[4],"tags":[302,462,466,523],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"If you add a &#039;group&#039; to a RethinkDB query, it will behave similarly to a similar query in SQL, except that you need to &#039;ungroup&#039; it afterward: r.db(&#039;test&#039;) .table(&#039;salaries&#039;) .group(&#039;role&#039;) .sum(&#039;salary&#039;) .ungroup() .map({Title: r.row(&#039;group&#039;), Amount: r.row(&#039;reduction&#039;)}) Amount Title 1 403000 null 2 217000 Designer 3 425000 Manager If, however, you group by two items, the\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"gary\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Gary Sieling - Software Engineer\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"RethinkDB: GROUP BY example - Gary Sieling\" \/>\n\t\t<meta property=\"og:description\" content=\"If you add a &#039;group&#039; to a RethinkDB query, it will behave similarly to a similar query in SQL, except that you need to &#039;ungroup&#039; it afterward: r.db(&#039;test&#039;) .table(&#039;salaries&#039;) .group(&#039;role&#039;) .sum(&#039;salary&#039;) .ungroup() .map({Title: r.row(&#039;group&#039;), Amount: r.row(&#039;reduction&#039;)}) Amount Title 1 403000 null 2 217000 Designer 3 425000 Manager If, however, you group by two items, the\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2016-03-08T02:41:48+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2016-03-08T02:41:48+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"RethinkDB: GROUP BY example - Gary Sieling\" \/>\n\t\t<meta name=\"twitter:description\" content=\"If you add a &#039;group&#039; to a RethinkDB query, it will behave similarly to a similar query in SQL, except that you need to &#039;ungroup&#039; it afterward: r.db(&#039;test&#039;) .table(&#039;salaries&#039;) .group(&#039;role&#039;) .sum(&#039;salary&#039;) .ungroup() .map({Title: r.row(&#039;group&#039;), Amount: r.row(&#039;reduction&#039;)}) Amount Title 1 403000 null 2 217000 Designer 3 425000 Manager If, however, you group by two items, the\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-group-example\\\/#blogposting\",\"name\":\"RethinkDB: GROUP BY example - Gary Sieling\",\"headline\":\"RethinkDB: GROUP BY example\",\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"},\"datePublished\":\"2016-03-08T02:41:48+00:00\",\"dateModified\":\"2016-03-08T02:41:48+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-group-example\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-group-example\\\/#webpage\"},\"articleSection\":\"Code Examples, javascript, reql, rethinkdb, sql\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-group-example\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.garysieling.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/#listItem\",\"name\":\"Code Examples\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/#listItem\",\"position\":2,\"name\":\"Code Examples\",\"item\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-group-example\\\/#listItem\",\"name\":\"RethinkDB: GROUP BY example\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-group-example\\\/#listItem\",\"position\":3,\"name\":\"RethinkDB: GROUP BY example\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/#listItem\",\"name\":\"Code Examples\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\",\"name\":\"Gary Sieling\",\"description\":\"Software Engineer\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/\",\"name\":\"gary\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-group-example\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/0be925276d848ffe98a6a9dc8cf33e67?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"gary\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-group-example\\\/#webpage\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-group-example\\\/\",\"name\":\"RethinkDB: GROUP BY example - Gary Sieling\",\"description\":\"If you add a 'group' to a RethinkDB query, it will behave similarly to a similar query in SQL, except that you need to 'ungroup' it afterward: r.db('test') .table('salaries') .group('role') .sum('salary') .ungroup() .map({Title: r.row('group'), Amount: r.row('reduction')}) Amount Title 1 403000 null 2 217000 Designer 3 425000 Manager If, however, you group by two items, the\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-group-example\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"datePublished\":\"2016-03-08T02:41:48+00:00\",\"dateModified\":\"2016-03-08T02:41:48+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/\",\"name\":\"Gary Sieling\",\"description\":\"Software Engineer\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"RethinkDB: GROUP BY example - Gary Sieling","description":"If you add a 'group' to a RethinkDB query, it will behave similarly to a similar query in SQL, except that you need to 'ungroup' it afterward: r.db('test') .table('salaries') .group('role') .sum('salary') .ungroup() .map({Title: r.row('group'), Amount: r.row('reduction')}) Amount Title 1 403000 null 2 217000 Designer 3 425000 Manager If, however, you group by two items, the","canonical_url":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/#blogposting","name":"RethinkDB: GROUP BY example - Gary Sieling","headline":"RethinkDB: GROUP BY example","author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"},"datePublished":"2016-03-08T02:41:48+00:00","dateModified":"2016-03-08T02:41:48+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/#webpage"},"isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/#webpage"},"articleSection":"Code Examples, javascript, reql, rethinkdb, sql"},{"@type":"BreadcrumbList","@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.garysieling.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/#listItem","name":"Code Examples"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/#listItem","position":2,"name":"Code Examples","item":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/#listItem","name":"RethinkDB: GROUP BY example"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/#listItem","position":3,"name":"RethinkDB: GROUP BY example","previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/#listItem","name":"Code Examples"}}]},{"@type":"Organization","@id":"https:\/\/www.garysieling.com\/blog\/#organization","name":"Gary Sieling","description":"Software Engineer","url":"https:\/\/www.garysieling.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author","url":"https:\/\/www.garysieling.com\/blog\/author\/gary\/","name":"gary","image":{"@type":"ImageObject","@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/0be925276d848ffe98a6a9dc8cf33e67?s=96&d=identicon&r=g","width":96,"height":96,"caption":"gary"}},{"@type":"WebPage","@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/#webpage","url":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/","name":"RethinkDB: GROUP BY example - Gary Sieling","description":"If you add a 'group' to a RethinkDB query, it will behave similarly to a similar query in SQL, except that you need to 'ungroup' it afterward: r.db('test') .table('salaries') .group('role') .sum('salary') .ungroup() .map({Title: r.row('group'), Amount: r.row('reduction')}) Amount Title 1 403000 null 2 217000 Designer 3 425000 Manager If, however, you group by two items, the","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/#breadcrumblist"},"author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"creator":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"datePublished":"2016-03-08T02:41:48+00:00","dateModified":"2016-03-08T02:41:48+00:00"},{"@type":"WebSite","@id":"https:\/\/www.garysieling.com\/blog\/#website","url":"https:\/\/www.garysieling.com\/blog\/","name":"Gary Sieling","description":"Software Engineer","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Gary Sieling - Software Engineer","og:type":"article","og:title":"RethinkDB: GROUP BY example - Gary Sieling","og:description":"If you add a 'group' to a RethinkDB query, it will behave similarly to a similar query in SQL, except that you need to 'ungroup' it afterward: r.db('test') .table('salaries') .group('role') .sum('salary') .ungroup() .map({Title: r.row('group'), Amount: r.row('reduction')}) Amount Title 1 403000 null 2 217000 Designer 3 425000 Manager If, however, you group by two items, the","og:url":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/","article:published_time":"2016-03-08T02:41:48+00:00","article:modified_time":"2016-03-08T02:41:48+00:00","twitter:card":"summary_large_image","twitter:title":"RethinkDB: GROUP BY example - Gary Sieling","twitter:description":"If you add a 'group' to a RethinkDB query, it will behave similarly to a similar query in SQL, except that you need to 'ungroup' it afterward: r.db('test') .table('salaries') .group('role') .sum('salary') .ungroup() .map({Title: r.row('group'), Amount: r.row('reduction')}) Amount Title 1 403000 null 2 217000 Designer 3 425000 Manager If, however, you group by two items, the"},"aioseo_meta_data":{"post_id":"3323","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2023-02-04 16:41:48","updated":"2026-07-06 01:34:53","ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.garysieling.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/\" title=\"Code Examples\">Code Examples<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tRethinkDB: GROUP BY example\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.garysieling.com\/blog"},{"label":"Code Examples","link":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/"},{"label":"RethinkDB: GROUP BY example","link":"https:\/\/www.garysieling.com\/blog\/rethinkdb-group-example\/"}],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3323"}],"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=3323"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3323\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}