Aggregates

Build 1501 on 14/Nov/2017  This topic last edited on: 25/Aug/2014, at 13:11

It is possible to 'aggregate' column values, to generate summary reports containing totals. Modifying the report options above adding:

    . . .

    </Columns>

    <Aggregates>

      <Aggregate ColumnName="Type"/>

      <Aggregate ColumnName="Id" Op="Count"/>

      <Aggregate ColumnName="Size" Op="Sum"/>

    </Aggregates>

  </ReportOptions>

and re-generating the reports produces:

  <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <r>

      <Type>audio</Type>

      <IdCount>7</IdCount>

      <SizeSum>57564982</SizeSum>

    </r>

    <r>

      <Type>image</Type>

      <IdCount>6</IdCount>

      <SizeSum>1027281</SizeSum>

    </r>

    <r>

      <Type>story</Type>

      <IdCount>8</IdCount>

      <SizeSum>395</SizeSum>

    </r>

    <r>

      <Type>video</Type>

      <IdCount>1</IdCount>

      <SizeSum>376002</SizeSum>

    </r>

  </root>

i.e. the number and total size of all the objects divided by type.

The definitions of each aggregate column specifies the name of the target column to aggregate ('ColumnName') and the operation to perform ('Op'). The possible operations are:

Group - group the result by the distinct values in the column

Count - count the values in the column

Sum - compute the sum - i.e. the total of all the values in the column

Average - computes the average of all the values in the column

Min - computes the minimum of all the values in the column

Max - computes the maximum of all the values in the column

The default operation is 'Group' - hence in the example above the aggregate on the Type column did not specify any operation.

The default name of the resulting aggregate column in the report is the name of the original column if the operation is Group, otherwise it is the name of the original column plus the name of the operation (hence for example 'IdCount' in the example output above).

It is possible to specify a different name for the aggregate column using the 'Name' attribute - e.g.

   . . .

   <Aggregate Name="N" ColumnName="Id" Op="Count"/>

   . . .

will output a column named 'N' instead than 'IdCount'.

Null values are ignored when computing aggregates - except for the 'Group' operation, where they are considered as a special value different from all other values.

If all the values being aggregated are null the resulting aggregate column will be null as well - in such a case the column will still be placed in the output XML but marked as null using 'xsi:nil' attribute.

Aggregate sort

It is possible to sort the aggregates in the output. Modifying the report options above adding:

    </Aggregates>

    <SortAggregates>

      <Sort AggregateName="IdCount" Ascending="false" />

    </SortAggregates>

  </ReportOptions>

will output the same aggregates but sorted by object count - from the object type with most objects down to the one with less.

It is possible to specify multiple aggregate column to sort by - e.g.

    </Aggregates>

    <SortAggregates>

      <Sort AggregateName="IdCount" Ascending="false" />

      <Sort AggregateName="Type" Ascending="true" />

    </SortAggregates>

  </ReportOptions>

will sort first by number of objects and them by type name (alphabetically) amongst all types with the same number of objects.

The 'Ascending' attribute controls the direction of the sort, and by default is 'true'.