The order of result records returned by an aggregation operation can be controlled using ORDER BY operators.
Records can be ordered by any of their property, dimension, or derived values, ascending or descending, in any combination.
Example 6. Examples with ORDER BY operators
For example, to order "SaleRep" aggregates by their total "Amount" values, the following Java API calls would be used:
Statement reps = new Statement(); reps.setName("Reps"); GroupByList groupBys = new GroupByList(); groupBys.add(new GroupBy("SalesRep")); reps.setGroupByList(groupBys); Expr e = new ExprAggregate(ExprAggregate.SUM, new ExprKey("Amount")); SelectList selects = new SelectList(); selects.add(new Select("Total",e)); reps.setSelectList(selects); OrderByList o = new OrderByList(); o.add(new OrderBy("Total", false)); reps.setOrderByList(o);
The same example in the text-based syntax is:
DEFINE Reps AS SELECT SUM(Amount) AS Total GROUP BY SalesRep ORDER BY Total DESC