{"id":3336,"date":"2016-03-09T01:51:36","date_gmt":"2016-03-09T01:51:36","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3336"},"modified":"2016-03-09T01:51:36","modified_gmt":"2016-03-09T01:51:36","slug":"rethinkdb-pivot-table-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-example\/","title":{"rendered":"RethinkDB: Pivot Table Example"},"content":{"rendered":"<p>Making a pivot table is a relatively common reporting task. I thought it&#8217;d be interesting to demonstrate how to do this with RethinkDB, as it shows how to build a complex query.<\/p>\n<p>In my sample data, we&#8217;ll use a table that lists salary information for a list of people. We&#8217;ll generate a pivot table that shows average salary by department, role and level.<\/p>\n<p>The first step is to compute all the averages, so that we can apply the pivot:<\/p>\n<pre lang=\"javascript\">r.db('test')\n .table('salaries')\n .group('Department', 'role', 'level')\n .avg('salary')\n .ungroup()\n<\/pre>\n<p>In SQL, this would have been equivalent to:<\/p>\n<pre lang=\"sql\">SELECT Department, role, level, avg(salary)\nGROUP BY 1, 2, 3\n<\/pre>\n<p>Notice how in SQL this would only return rows for values that exist. Values for each combination of role and level are in different rows, rather than columns.<\/p>\n<p>To start the pivot, we first turn each result into an array:<\/p>\n<pre lang=\"javascript\"> ...\n .map( (doc) =>\n    [doc('group')(0), \n     doc('group')(1), \n     doc('group')(2), \n     doc('reduction')]\n )\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\"><em>value<\/em><\/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\">\n[&#8220;Maintenance&#8221;, &#8220;Engineer&#8221;, 2, 72000]\n<\/div>\n<\/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\">\n<p>[&#8220;Maintenance&#8221;, &#8220;Manager&#8221;, 1, 120000]<\/p>\n<\/div>\n<\/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\">\n<p>[&#8220;New Product Development&#8221;, &#8220;Designer&#8221;, 1, 40500]<\/p>\n<\/div>\n<\/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\">\n<p>[&#8220;New Product Development&#8221;, &#8220;Designer&#8221;, 2, 64500]<\/p>\n<\/div>\n<\/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\">\n<p>[&#8220;New Product Development&#8221;, &#8220;Engineer&#8221;, 1, 40000]<\/p>\n<\/div>\n<\/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\">\n<p>[&#8220;New Product Development&#8221;, &#8220;Engineer&#8221;, 2, 65000]<\/p>\n<\/div>\n<\/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\">8<\/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\">\n<p>[&#8220;New Product Development&#8221;, &#8220;Engineer&#8221;, 3, 112500]<\/p>\n<\/div>\n<\/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\">9<\/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\">\n<p>[&#8220;New Product Development&#8221;, &#8220;Manager&#8221;, 1, 103500]<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>This allows us to apply transformations to all the row values, so we can use some as column names.<\/p>\n<p>Once we do this, we can combine the role and level fields into column names &#8211; i.e. to produce names like &#8220;Engineer_1&#8221;, &#8220;Engineer_2&#8221;, &#8220;Manager_1&#8221;, &#8220;Manager_2&#8221;, and so on.<\/p>\n<pre lang=\"javascript\">...\n  .map( (doc) =>\n    r.object(\n     'Department', \n     doc(0), \n     doc(1).add('_').add(doc(2).coerceTo('string')), \n     doc(3))\n  )\n<\/pre>\n<p>The above expression is requires you to coerce numeric values to strings &#8211; &#8220;add&#8221;expects to operate on two values of the same type.<\/p>\n<p>If you get null reference errors, you can start this with &#8220;expr(&#8221;).add(&#8230;)&#8221; to ensure the absence of null values.<\/p>\n<div id=\"sticky-wrap\">\n<div id=\"main-container\">\n<div id=\"cluster\">\n<div id=\"dataexplorer\">\n<div class=\"section\">\n<div class=\"results_container\">\n<div class=\"result_view\">\n<div class=\"results_header\">\n<p class=\"metadata feed_paused\">The above query will return a result that looks a bit odd, but almost a pivot table:<\/p>\n<p class=\"metadata feed_paused\">9 rows returned in 4ms.<\/p>\n<\/div>\n<div class=\"wrapper_scrollbar\"><\/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\">Department<\/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\">Engineer_2<\/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\">Manager_1<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-3\">Designer_1<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-4\">Designer_2<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-6\">Engineer_1<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-7\">Engineer_3<\/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_string\">Maintenance<\/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\">72000<\/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_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-6 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_undefined\">undefined<\/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\">Maintenance<\/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_undefined\">undefined<\/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\">120000<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-6 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_undefined\">undefined<\/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\">New Product Development<\/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_undefined\">undefined<\/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_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_num\">40500<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-6 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_undefined\">undefined<\/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\">New Product Development<\/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_undefined\">undefined<\/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_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_num\">64500<\/div>\n<\/div>\n<\/td>\n<td class=\"col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-6 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_undefined\">undefined<\/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\">New Product Development<\/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_undefined\">undefined<\/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_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-6 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_undefined\">undefined<\/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\">New Product Development<\/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_undefined\">undefined<\/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_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-6 jta_num\">40000<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_undefined\">undefined<\/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\">New Product Development<\/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\">65000<\/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_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-6 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_undefined\">undefined<\/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\">8<\/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\">New Product Development<\/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_undefined\">undefined<\/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_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-6 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_num\">112500<\/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\">9<\/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\">New Product Development<\/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_undefined\">undefined<\/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\">103500<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-6 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_undefined\">undefined<\/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\">\u00a0We combine the rows by grouping by the column we want to keep as a label (in this case, &#8216;Department&#8217;). Then, do a\u00a0reduction using the\u00a0&#8216;merge&#8217; function\u00a0to combine rows. Since each column is only defined in a single row, it&#8217;s safe to combine these (this will occur in whatever order RethinkDB chooses)<\/div>\n<pre lang=\"javascript\">...\n .group('Department')\n .reduce(\n   (a, b) => a.merge(b)\n )\n<\/pre>\n<p>This produces a nice looking result. Note that we still have &#8216;reduction&#8217; bit.<\/p>\n<div id=\"sticky-wrap\">\n<div id=\"main-container\">\n<div id=\"cluster\">\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\">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.Department<\/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\">reduction.Engineer_2<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-3\">reduction.Manager_1<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-4\">reduction.Designer_1<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-5\" data-col=\"5\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-5\">reduction.Designer_2<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-7\">reduction.Engineer_1<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-8\" data-col=\"8\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-8\">reduction.Engineer_3<\/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_string\">Maintenance<\/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\">Maintenance<\/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\">72000<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_num\">120000<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-5\" data-col=\"5\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-5 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-8\" data-col=\"8\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-8 jta_undefined\">undefined<\/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\">New Product Development<\/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\">New Product Development<\/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\">65000<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_num\">103500<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_num\">40500<\/div>\n<\/div>\n<\/td>\n<td class=\"col-5\" data-col=\"5\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-5 jta_num\">64500<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_num\">40000<\/div>\n<\/div>\n<\/td>\n<td class=\"col-8\" data-col=\"8\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-8 jta_num\">112500<\/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<div>We can fix this with one more transformation, to remove the effects of &#8216;group&#8217; and just get the child objects:<\/div>\n<div>\n&#8230;<br \/>\n.ungroup()<br \/>\n.map( (doc) => doc(&#8216;reduction&#8217;) )<\/div>\n<div><\/div>\n<div>\n<div id=\"sticky-wrap\">\n<div id=\"main-container\">\n<div id=\"cluster\">\n<div id=\"dataexplorer\">\n<div class=\"section\">\n<div class=\"results_container\">\n<div class=\"result_view\">\n<div class=\"results_header\">\n<p class=\"metadata feed_paused\">2 rows returned in 4ms.<\/p>\n<\/div>\n<div class=\"wrapper_scrollbar\"><\/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\">Department<\/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\">Engineer_2<\/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\">Manager_1<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-3\">Designer_1<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-4\">Designer_2<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-6\">Engineer_1<\/div>\n<\/div>\n<\/td>\n<td class=\"td_attr col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_attr value-7\">Engineer_3<\/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_string\">Maintenance<\/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\">72000<\/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\">120000<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-6 jta_undefined\">undefined<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_undefined\">undefined<\/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\">New Product Development<\/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\">65000<\/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\">103500<\/div>\n<\/div>\n<\/td>\n<td class=\"col-3\" data-col=\"3\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-3 jta_num\">40500<\/div>\n<\/div>\n<\/td>\n<td class=\"col-4\" data-col=\"4\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-4 jta_num\">64500<\/div>\n<\/div>\n<\/td>\n<td class=\"col-6\" data-col=\"6\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-6 jta_num\">40000<\/div>\n<\/div>\n<\/td>\n<td class=\"col-7\" data-col=\"7\">\n<div class=\"click_detector\">\n<div class=\"jta_value value-7 jta_num\">112500<\/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<div>And here is the final query, in all it&#8217;s glory:<\/div>\n<pre lang=\"javascript\">r.db('test')\n .table('salaries')\n .group('Department', 'role', 'level')\n .avg('salary')\n .ungroup()\n .map( (doc) => [\n   doc('group')(0), \n   doc('group')(1), \n   doc('group')(2), \n   doc('reduction')\n  ])\n .map( (doc) =>\n   r.object(\n     'Department', \n     doc(0), \n     doc(1).add('_').add(doc(2).coerceTo('string')), \n     doc(3))\n )\n .group('Department')\n .reduce(\n   (a, b) => a.merge(b)\n )\n .ungroup()\n .map( \n   (doc) => doc('reduction') \n )\n<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Making a pivot table is a relatively common reporting task. I thought it&#8217;d be interesting to demonstrate how to do this with RethinkDB, as it shows how to build a complex query. In my sample data, we&#8217;ll use a table that lists salary information for a list of people. We&#8217;ll generate a pivot table that &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;RethinkDB: Pivot Table 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":[204,302,435,461,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=\"Making a pivot table is a relatively common reporting task. I thought it&#039;d be interesting to demonstrate how to do this with RethinkDB, as it shows how to build a complex query. In my sample data, we&#039;ll use a table that lists salary information for a list of people. We&#039;ll generate a pivot table that\" \/>\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-pivot-table-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: Pivot Table Example - Gary Sieling\" \/>\n\t\t<meta property=\"og:description\" content=\"Making a pivot table is a relatively common reporting task. I thought it&#039;d be interesting to demonstrate how to do this with RethinkDB, as it shows how to build a complex query. In my sample data, we&#039;ll use a table that lists salary information for a list of people. We&#039;ll generate a pivot table that\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-example\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2016-03-09T01:51:36+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2016-03-09T01:51:36+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"RethinkDB: Pivot Table Example - Gary Sieling\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Making a pivot table is a relatively common reporting task. I thought it&#039;d be interesting to demonstrate how to do this with RethinkDB, as it shows how to build a complex query. In my sample data, we&#039;ll use a table that lists salary information for a list of people. We&#039;ll generate a pivot table that\" \/>\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-pivot-table-example\\\/#blogposting\",\"name\":\"RethinkDB: Pivot Table Example - Gary Sieling\",\"headline\":\"RethinkDB: Pivot Table Example\",\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"},\"datePublished\":\"2016-03-09T01:51:36+00:00\",\"dateModified\":\"2016-03-09T01:51:36+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-pivot-table-example\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-pivot-table-example\\\/#webpage\"},\"articleSection\":\"Code Examples, etl, javascript, pivot table, reporting, reql, rethinkdb, sql\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-pivot-table-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-pivot-table-example\\\/#listItem\",\"name\":\"RethinkDB: Pivot Table Example\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-pivot-table-example\\\/#listItem\",\"position\":3,\"name\":\"RethinkDB: Pivot Table 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-pivot-table-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-pivot-table-example\\\/#webpage\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-pivot-table-example\\\/\",\"name\":\"RethinkDB: Pivot Table Example - Gary Sieling\",\"description\":\"Making a pivot table is a relatively common reporting task. I thought it'd be interesting to demonstrate how to do this with RethinkDB, as it shows how to build a complex query. In my sample data, we'll use a table that lists salary information for a list of people. We'll generate a pivot table that\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/rethinkdb-pivot-table-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-09T01:51:36+00:00\",\"dateModified\":\"2016-03-09T01:51:36+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: Pivot Table Example - Gary Sieling","description":"Making a pivot table is a relatively common reporting task. I thought it'd be interesting to demonstrate how to do this with RethinkDB, as it shows how to build a complex query. In my sample data, we'll use a table that lists salary information for a list of people. We'll generate a pivot table that","canonical_url":"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-example\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-example\/#blogposting","name":"RethinkDB: Pivot Table Example - Gary Sieling","headline":"RethinkDB: Pivot Table Example","author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"},"datePublished":"2016-03-09T01:51:36+00:00","dateModified":"2016-03-09T01:51:36+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-example\/#webpage"},"isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-example\/#webpage"},"articleSection":"Code Examples, etl, javascript, pivot table, reporting, reql, rethinkdb, sql"},{"@type":"BreadcrumbList","@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-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-pivot-table-example\/#listItem","name":"RethinkDB: Pivot Table Example"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-example\/#listItem","position":3,"name":"RethinkDB: Pivot Table 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-pivot-table-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-pivot-table-example\/#webpage","url":"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-example\/","name":"RethinkDB: Pivot Table Example - Gary Sieling","description":"Making a pivot table is a relatively common reporting task. I thought it'd be interesting to demonstrate how to do this with RethinkDB, as it shows how to build a complex query. In my sample data, we'll use a table that lists salary information for a list of people. We'll generate a pivot table that","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-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-09T01:51:36+00:00","dateModified":"2016-03-09T01:51:36+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: Pivot Table Example - Gary Sieling","og:description":"Making a pivot table is a relatively common reporting task. I thought it'd be interesting to demonstrate how to do this with RethinkDB, as it shows how to build a complex query. In my sample data, we'll use a table that lists salary information for a list of people. We'll generate a pivot table that","og:url":"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-example\/","article:published_time":"2016-03-09T01:51:36+00:00","article:modified_time":"2016-03-09T01:51:36+00:00","twitter:card":"summary_large_image","twitter:title":"RethinkDB: Pivot Table Example - Gary Sieling","twitter:description":"Making a pivot table is a relatively common reporting task. I thought it'd be interesting to demonstrate how to do this with RethinkDB, as it shows how to build a complex query. In my sample data, we'll use a table that lists salary information for a list of people. We'll generate a pivot table that"},"aioseo_meta_data":{"post_id":"3336","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: Pivot Table 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: Pivot Table Example","link":"https:\/\/www.garysieling.com\/blog\/rethinkdb-pivot-table-example\/"}],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3336"}],"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=3336"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3336\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}