Aggregate Functions PivotData C# examples


NReco.PivotData implements typical summary functions: count, count unique, sum, average, min, max, variance/stddev, formula. It is possible to implement custom aggregator if needed.

Aggregate functions

In PivotData library aggregator is represented by IAggregatorFactory components that can create individual IAggregator accumulators. Library implements the following aggregate functions:

Factory component Type of Value Description Example
CompositeAggregatorFactory object[] Used for collecting several measures; accepts list of aggregator factories.
new CompositeAggregatorFactory(
  new CountAggregatorFactory(),
  new SumAggregatorFactory("amount")
)
CountAggregatorFactory uint Counts number of rows from facts table
new CountAggregatorFactory()
CountUniqueAggregatorFactory int Counts number of unique values from specified facts table column
new CountUniqueAggregatorFactory(
  "company_title")
SumAggregatorFactory decimal Computes the sum of the specified column values from facts table
new SumAggregatorFactory("amount")
MinAggregatorFactory (type of specified field) Computes the minimum of the specified column values from facts table
new MinAggregatorFactory("age")
MaxAggregatorFactory (type of specified field) Computes the maximum of the specified column values from facts table
new MaxAggregatorFactory("age")
AverageAggregatorFactory decimal Computes the average of the specified column values from facts table
new AverageAggregatorFactory("age")
ListUniqueAggregatorFactory ICollection Collects only unique values from specified facts table column
new ListUniqueAggregatorFactory("age")
ListAggregatorFactory ICollection Collects all references (or values if field is specified) to the source fact objects (like LINQ GroupBy).
new ListAggregatorFactory()
new ListAggregatorFactory("name")
VarianceAggregatorFactory double Computes mean/variance/sample variance/standard deviation/sample std-dev
new VarianceAggregatorFactory(
  "exchange_rate", 
  VarianceAggregatorValueType.SampleStdDevValue) )
FormulaAggregatorFactory (depends on formula result) Compute custom formula value by arguments of specified aggregators
new FormulaAggregatorFactory(
 "Actual/Estimated", 
 (argValues) => {
  var actual = Convert.ToDecimal(argValues[0]);
  var estimated = Convert.ToDecimal(argValues[1]);
  return actual/estimated;
 },
 new [] {
  new SumAggregatorFactory("actual"),
  new SumAggregatorFactory("estimated")
 } )

It is possible to use custom aggregator (single-pass summary function) by implementing IAggregatorFactory/IAggregator interfaces.


Next section: OLAP operations (query and filter)