.groupby(columnName.rules)

groupby rules provides the ability to group a column by a set of rules. This feature is currently only implemented on numeric column types.

The groupby rules are composed of a pipe ( | ) separated list of min/max pairs with the option to use the keyword “catch” as a catch all (* in SQL).

Rules do not have to be in numeric order but catch is required to group non-matching rows.

Syntax .groupby(columnName.rules.min1/max1|min2/max2|min3/max3) Example
  1. var q = ml.query().from('maplarge/Donors');
  2. q.select('Amount.sum','Amount.count','Amount.key','Amount');
  3. q.groupby('Amount.rules.0/1000|1000/10000|10000/100000|100000/1000000|catch');
  4. q.run(function (data, query);
  5. console.log(data);
  6. });
Demo Total State Donations, Ordered by State
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="/JS"></script>
  4. <script type="text/javascript">
  5. ml.onload(function () {
  6.  
  7. var q = ml.query().from('maplarge/Donors');
  8.  
  9. // choose the columns to return
  10. q.select('Amount.sum','Amount.count','Amount.key','Amount');
  11.  
  12. // group the data
  13. q.groupby('Amount.rules.0/1000|1000/10000|10000/100000|100000/1000000|catch');
  14.  
  15. // execute the query and process the return with the callback
  16. q.run(function (data, query) {
  17.  
  18. // get a reference to the element that will house our HTML table
  19. var dataDiv = document.getElementById("dataDiv");
  20.  
  21. // build the HTML table
  22. var html = '<table><tr><th></th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th></tr>';
  23. html += '<tr><td>Donation Count</td>';
  24.  
  25. for(var i = 0; i < data.data.Amount_Count.length; i++){
  26. html += '<td>' + ml.util.addCommas(data.data.Amount_Count[i]) + '</td>';
  27. }
  28. html += '</tr>';
  29.  
  30. html += '<tr><td>Donation Sum</td>';
  31.  
  32. for(var i = 0; i < data.data.Amount_Sum.length; i++){
  33. html += '<td>$ ' + ml.util.addCommas(data.data.Amount_Sum[i]) + '</td>';
  34. }
  35. html += '</tr>';
  36.  
  37. html += '</table>';
  38. dataDiv.innerHTML = html;
  39. });
  40.  
  41. });
  42. </script>
  43. <style>
  44. table tr:nth-child(even) {
  45. background-color: #eee;
  46. }
  47. table tr:nth-child(odd) {
  48. background-color:#fff;
  49. }
  50. table th {
  51. background-color: rgb(82, 112, 158);
  52. color: white;
  53. }
  54. table td {
  55. text-align:right;
  56. padding: 5px;
  57. }
  58. table tr td:nth-child(1) {
  59. text-align:left;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <div id="dataDiv"></div>
  65. </body>
  66. </html>