MarkLogic 10 Product Documentation
op:sum

op:sum(
   $outCol as item(),
   $inCol as item(),
   [$options as map:map?]
) as map:map

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 op:group-by 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
$outCol The name to be used for the aggregated column.
$inCol 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

xquery version "1.0-ml";

import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

let $employees := op:from-view("main", "employees")
let $expenses  := op:from-view("main", "expenses")
let $totalexpenses  := op:col("totalexpenses")
return $employees
   => op:join-inner($expenses, op:on(
                    op:view-col("employees", "EmployeeID"),
                    op:view-col("expenses", "EmployeeID")))
   => op:group-by(op:view-col("employees", "EmployeeID"),
                 ("FirstName", "LastName",
                  op:sum($totalexpenses,
                  op:view-col("expenses", "Amount"))))
   => op:order-by(op:view-col("employees", "EmployeeID"))
   => op:result()
  
Powered by MarkLogic Server | Terms of Use | Privacy Policy