Table of Contents

Class Average

Namespace
YndigoBlue.Velocity.Functions
Assembly
YndigoBlue.Velocity.dll

Represents the AVG aggregate function that calculates the arithmetic mean of numeric values.

public class Average : Function, ICheckItem, IDefaultItem, IFilterItem, IElement
Inheritance
Average
Implements

Remarks

The AVG function computes the average by summing all non-null values and dividing by the count of non-null values. Null values are excluded from both the sum and count. The result is typically a decimal or floating-point value even when averaging integers. Commonly used with GROUP BY to calculate averages per group.

Examples

Calculate the average order value per customer:

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

var query = new Query()
    .Select([ orders["customer_id"], new Expression("avg_order_value", new Average(orders["amount"])) ])
    .From(orders)
    .GroupBy([ orders["customer_id"] ]);

var results = manager.Retrieve(query);

Calculate the average effective price using a composed expression (AVG(quantity * unit_price)):

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

var query = new Query()
    .Select([
        orders["product_id"],
        new Expression("avg_line_value", new Average([ orders["quantity"], new ArithmeticOperator(ArithmeticType.Multiply), orders["unit_price"] ]))
    ])
    .From(orders)
    .GroupBy([ orders["product_id"] ]);

var results = manager.Retrieve(query);

Constructors

Average(IEnumerable<IElement>)

Initializes a new instance of AVG for a composed expression (e.g. AVG(quantity * unit_price)).

Average(IElement)

Initializes a new instance of AVG for a single element.

Average(Column)

Initializes a new instance of AVG for a specific column.