Loading TOC...

op.sum

op.sum(
   $aggColName as String,
   $columndef as String,
   [$options as String]
) as aggregatedef

Summary

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.
$options The options can take a values key with a distinct value to average the distinct values of the column.

Example


// 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 iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.