Loading TOC...

MarkLogic Server 11.0 Product Documentation
ModifyPlan.prototype.groupToArrays

ModifyPlan.prototype.groupToArrays(
   groups as namedGroupdef[],
   [aggregates as aggregatedef[]]
) as ModifyPlan

Summary

This method performs multiple group-by operations on a row set and produces a single row with a column for each group having an array value whose items are the rows for the group. Each item is an object with properties for the group keys and aggregates.

The aggregates for the operation are specified as the second parameter instead of in a prototype.select operation (unlike SQL).

Parameters
groups The sets of grouping keys. Each group is specified with the op.namedGroup function, which supplies the name for the group column in the output row. Each group must have a unique set of keys but multiple groups can have the same key.

As a convenience, the parameter also accepts unnamed groups with the op.group, op.rollup, or op.cube functions or (for a group with a single key) a column named with a string or a column parameter function such as op.col or constructed from an expression with the op.as function.

The row objects produced by the group-to-arrays operation include the key columns from each group. A group can be empty to group over all of the rows in the row set.

aggregates This parameter specifies either columns to sample or aggregate functions to apply to a column for all of the rows in the group. Sampled columns can be existing columns or new columns created by an expression specified with op.as. Often a sampled column might have a constant value within the group such as a title or label closely associated with a numeric identifier used as the grouping key.

Usage Notes

The groupToArrays operation differs from the groupBy operation as follows:

groupToArrays is a method of the following classes:

See Also

Example

// Calculate the total expenses separately for each category and for each location.

const op = require('/MarkLogic/optic');

op.fromView('main', 'expenses')
   .groupToArrays(
       [op.namedGroup('CategoryTotal', 'Category'),
           op.namedGroup('LocationTotal', 'Location'),
           op.namedGroup('AllTotal')
           ],
       op.sum("TotalAmount", "Amount")
       )
   .result();
  

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.