Reference > Functions > Aggregate

Var

  • 2005, 2008, 2012, 2014, 2016, 2017, 2019, 2022

Var (Variance) Aggregate Function

Use the Var function to return the statistical variance for all values in the specified expression.

Syntax

db.fx.Var({expression})[.Distinct()]

Arguments

expression
– The field expression, composite element, or function result to use in calculating the variance.
Distinct()
– Each unique value is considered in calculating the variance value.

Returns

float

Examples

Select Statement

Select the variance of product shipping weights.

float result = db.SelectOne(
        db.fx.Var(dbo.Product.ShippingWeight)
    )
    .From(dbo.Product)
    .Execute();

Order By Clause

Select the variance of product shipping weights ordered by the variance of product shipping weights descending.

float result = db.SelectOne(
        db.fx.Var(dbo.Product.ShippingWeight)
    )
    .From(dbo.Product)
    .OrderBy(db.fx.Var(dbo.Product.ShippingWeight).Desc())
    .Execute();

Having Clause

Select the product categories of all products, grouped by product category type having an variance greater than 1.

IEnumerable<ProductCategoryType?> results = db.SelectMany(
        dbo.Product.ProductCategoryType
    )
    .From(dbo.Product)
    .GroupBy(dbo.Product.ProductCategoryType)
    .Having(db.fx.Var(dbo.Product.ShippingWeight) > 1)
    .Execute();
Previous
Sum

© 2024 dbExpression. All rights reserved.