Table of Contents

Class Aggregate

Namespace
YndigoBlue.Velocity.Model
Assembly
YndigoBlue.Velocity.dll

Represents an aggregate function operation (COUNT, SUM, AVG, MIN, MAX, STDDEV, VAR) that can be used in expressions.

public class Aggregate : IElement
Inheritance
Aggregate
Implements

Remarks

Aggregate performs aggregate calculations on sets of rows, typically used with GROUP BY clauses. While Function is the more common way to use aggregate functions, Aggregate provides a lower-level building block for complex scenarios where aggregates need to be composed with other elements in expressions.

The supported aggregate types are:

  • CountCounts the number of rows or non-NULL values.
  • SumCalculates the sum of values.
  • AverageCalculates the arithmetic mean of values.
  • MinFinds the minimum value.
  • MaxFinds the maximum value.
  • StdDevCalculates the standard deviation of values.
  • VarianceCalculates the variance of values.

For Count, Sum, Average, Min, and Max, prefer using Function with the corresponding FunctionType values, as dedicated subclasses provide a cleaner API. StdDev and Variance have no Function counterparts, so Aggregate is the correct way to use those two.

Examples

Using StdDev and Variance (must use Aggregate directly):

var schema = manager.LoadSchema("analytics");
var measurements = schema["measurements"];

var query = new Query()
    .Select([
        measurements["sensor_id"],
        new Expression("std_dev", new Aggregate(AggregateType.StdDev, measurements["reading"])),
        new Expression("variance", new Aggregate(AggregateType.Variance, measurements["reading"]))
    ])
    .From(measurements)
    .GroupBy(measurements["sensor_id"]);

var results = manager.Retrieve(query);

Using SUM(price * quantity) composed from multiple elements:

var schema = manager.LoadSchema("sales");
var orders = schema["orders"];

var query = new Query()
    .Select([
        orders["customer_id"],
        new Expression("revenue", new Aggregate(AggregateType.Sum, [ orders["price"], new ArithmeticOperator(ArithmeticType.Multiply), orders["quantity"] ]))
    ])
    .From(orders)
    .GroupBy(orders["customer_id"]);

var results = manager.Retrieve(query);

Constructors

Aggregate(AggregateType)

Creates a new aggregate without any elements.

Aggregate(AggregateType, IEnumerable<IElement>)

Creates a new aggregate with multiple elements.

Aggregate(AggregateType, IElement)

Creates a new aggregate with a single element.

Properties

AggregateType

Gets or sets the type of aggregate operation (Count, Sum, Average, Min, Max, StdDev, Variance).

Elements

Gets or sets the elements (columns, expressions, etc.) that the aggregate operates on.