.groupby()
The groupby method can be used in conjunction with aggregate functions to group the result-set by one or more columns. Syntax .groupby(columnName: string) Aggregate Functions

When doing a groupby query aggregate functions can be used in select statements to request additional aggregate data for a column.

NameDescription
avgThe average of the grouped column values. Average can only be used on numeric columns.
sumThe sum of the grouped column values. Sum can only be used on numeric columns.
countThe count of the grouped column values. Count can only be used on numeric columns.
minThe max of the grouped column values. Min can only be used on numeric columns.
maxThe min of the grouped column values. Max can only be used on numeric columns.
keyIndexing value used for groupby.count and groupby.value.
Example
  1. var q = ml.query().from('Example/CampaignDonors');
  2. q.select('State','Amount.sum');
  3. q.groupby('State');
  4. q.run(function (data, query);
  5. console.log(data);
  6. });
Demo
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="/JS"></script>
  4. <script type="text/javascript">
  5. ml.onload(function () {
  6. var q = ml.query().from('Example/CampaignDonors');
  7.  
  8. // choose the columns to return
  9. q.select('State','Amount.sum');
  10.  
  11. // group the data
  12. q.groupby('State');
  13.  
  14. // execute the query and process the return with the callback
  15. q.run(function (data, query) {
  16.  
  17. // get a reference to the element that will house our HTML table
  18. var dataDiv = document.getElementById("dataDiv");
  19.  
  20.  
  21. // build the HTML table
  22. var html = '<table><tr><th>State</th><th>Amount</th></tr>';
  23.  
  24. for(var i = 0; i < data.data.State.length; i++){
  25. html += '<tr><td>' + data.data.State[i] + '</td>';
  26. html += '<td>' + data.data.Amount_Sum[i] + '</td></tr>';
  27. }
  28.  
  29. html += '</table>';
  30. dataDiv.innerHTML = html;
  31. });
  32.  
  33. });
  34. </script>
  35. <style>
  36. table tr:nth-child(even) {
  37. background-color: #eee;
  38. }
  39. table tr:nth-child(odd) {
  40. background-color:#fff;
  41. }
  42. table th {
  43. background-color: rgb(63, 94, 144);
  44. color: white;
  45. padding: 5px;
  46. }
  47. table td {
  48. padding: 5px;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <div id="dataDiv"></div>
  54. </body>
  55. </html>
Special groupbys