
op.sum( aggColName as String, columndef as ColumnIdentifier, [options as String] ) as aggregatedef
This function adds the non-null values of the column for the rows in the group or row set. The result is used for building the parameters used by the prototype.groupBy function.
The op.sum function differs from the op.add function in that it operates on operates on a group of rows, rather than multiple column values in a row.
| Parameters | |
|---|---|
| aggColName | The name to be used for the aggregated column. |
| columndef | The column with the values to add. The column can be named with a string or a column function such as op.col, op.viewCol, or op.schemaCol, or constructed from an expression with the op.as function. |
| options |
The options can take a values key with a 'distinct' value
to sum the distinct values of the column.
|
// Calculate the total expenses for each employee and return results in order of employee number.
const op = require('/MarkLogic/optic');
const employees = op.fromView('main', 'employees');
const expenses = op.fromView('main', 'expenses');
const totalexpenses = op.col('totalexpenses');
const Plan =
employees.joinInner(expenses, op.on(employees.col('EmployeeID'), expenses.col('EmployeeID')))
.groupBy(employees.col('EmployeeID'), ['FirstName', 'LastName',
op.sum(totalexpenses, expenses.col('Amount'))])
.orderBy('EmployeeID')
Plan.result();
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.