.groupby()
The groupby method can be used in conjunction with aggregate functions to group the result-set by one or more columns..groupby(columnName: string)
When doing a groupby query aggregate functions can be used in select statements to request additional aggregate data for a column.
Name | Description |
---|---|
avg | The average of the grouped column values. Average can only be used on numeric columns. |
sum | The sum of the grouped column values. Sum can only be used on numeric columns. |
count | The count of the grouped column values. Count can only be used on numeric columns. |
min | The max of the grouped column values. Min can only be used on numeric columns. |
max | The min of the grouped column values. Max can only be used on numeric columns. |
key | Indexing value used for groupby.count and groupby.value. |
var q = ml.query().from('Example/CampaignDonors'); q.select('State','Amount.sum'); q.groupby('State'); q.run(function (data, query); console.log(data); });
<html> <head> <script type="text/javascript" src="/JS"></script> <script type="text/javascript"> ml.onload(function () { var q = ml.query().from('Example/CampaignDonors'); // choose the columns to return q.select('State','Amount.sum'); // group the data q.groupby('State'); // execute the query and process the return with the callback q.run(function (data, query) { // get a reference to the element that will house our HTML table var dataDiv = document.getElementById("dataDiv"); // build the HTML table var html = '<table><tr><th>State</th><th>Amount</th></tr>'; for(var i = 0; i < data.data.State.length; i++){ html += '<tr><td>' + data.data.State[i] + '</td>'; html += '<td>' + data.data.Amount_Sum[i] + '</td></tr>'; } html += '</table>'; dataDiv.innerHTML = html; }); }); </script> <style> table tr:nth-child(even) { background-color: #eee; } table tr:nth-child(odd) { background-color:#fff; } table th { background-color: rgb(63, 94, 144); color: white; padding: 5px; } table td { padding: 5px; } </style> </head> <body> <div id="dataDiv"></div> </body> </html>