| 203 | | === Aggregation and Groupby === |
| 204 | | |
| 205 | | Aggregates can be constructed from field expressions |
| 206 | | |
| 207 | | The {{{select()}}} method accepts an object with arguments as (optional) second parameter, e.g. {{{limitby}}} and {{{orderby}}}: |
| 208 | | |
| 209 | | {{{#!js |
| | 203 | === Aggregates and Groupby === |
| | 204 | |
| | 205 | '''Aggregate expressions''' can be constructed from field expressions using operator functions, e.g.: |
| | 206 | |
| | 207 | {{{#!js |
| | 208 | // The total (sum) of the field 'value': |
| | 209 | var totalValue = table.$('value').sum(); |
| | 210 | }}} |
| | 211 | |
| | 212 | Currently supported operators are '''count''', '''sum''', '''avg''', '''min''', and '''max'''. |
| | 213 | |
| | 214 | Aggregate expressions can be used to extract the aggregate values from rows: |
| | 215 | |
| | 216 | {{{#!js |
| | 217 | // The total (sum) of the field 'value': |
| | 218 | var totalValue = table.$('value').sum(); |
| | 219 | ... |
| | 220 | // Access the total in a row |
| | 221 | var value = row.$(totalValue); |
| | 222 | }}} |
| | 223 | |
| | 224 | The {{{select()}}} method of Sets accepts a groupby option: |
| | 225 | |
| | 226 | {{{#!js |
| | 227 | // Extract the total of values per location |
| | 228 | var locationID = table.$('location_id'), |
| | 229 | totalValue = table.$('value').sum(); |